如何将元素从 Swift 中的 [UnsafeMutablePointer<UInt8>] 转换为 C++ 中的 UInt8 * [英] How to cast element from [UnsafeMutablePointer<UInt8>] in Swift to UInt8 * in C++
问题描述
我有以下代码无法编译,因为 XCode 不允许我将 NSArray 元素转换为 C++ 代码中的指针.XCode 给出的错误是:Assigning to 'UInt8 *' from incompatible type 'id'
.
I have the following code that will not compile because XCode won't let me cast a NSArray element into a pointer in my C++ code.
The error given by XCode is: Assigning to 'UInt8 *' from incompatible type 'id'
.
我应该如何将 [UnsafeMutablePointer<UInt8>]
类型的数组从 Swift 传递到 Objective-C++?
How am I supposed to pass an array of type [UnsafeMutablePointer<UInt8>]
from Swift to Objective-C++ ?
提前致谢
objcfunc.h
+ (void) call: (NSArray *) arr;
objcfunc.mm
+ (void) call: (NSArray *) arr {
UInt8 *buffer;
buffer = (UInt8 *) arr[0]; // doesn't work, XCode throws an error
unsigned char *image;
image = (unsigned char *) buffer;
processImage(image); // C++ function
}
迅捷
var arr: [UnsafeMutablePointer<UInt8>] = []
arr.append(someImage)
objcfunc.call(swiftArray: arr)
但是如果我不使用数组而直接传递指针,代码就可以正常工作:
But if I don't use an array and directly pass the pointer, the code works fine:
objcfunc.h
+ (void) callSingle: (UInt8 *) buf;
objcfunc.mm
+(void) callSingle: (UInt8 *) buf {
unsigned char *image;
image = (unsigned char *) buf; // works fine
processImage(image);
}
迅捷
let x: UnsafeMutablePointer<UInt8> buf;
// initialize buf
objcfunc.callSingle(buf);
推荐答案
NSArray
是一个 Objective-C 对象数组.因此,您需要传递桥接到 Objective-C 类型的类型实例数组.我不确定 Swift 的 UnsafeMutablePointer
结构是否被桥接.
NSArray
is an array of Objective-C objects. So, you need to pass an array of instances of types that are bridged to Objective-C types. I'm not sure that Swift's UnsafeMutablePointer
struct is bridged.
因为在这种情况下您传递的是图像缓冲区数组(如果我理解正确的话),您可能需要考虑使用 NSData
或 Data
,而不是 UnsafeMutablePointer
用于每个图像缓冲区.这些类型专门用于处理字节数组,这就是图像缓冲区;看https://developer.apple.com/reference/foundation/nsdata 和https://developer.apple.com/reference/foundation/data
Because in this case you are passing an array of image buffers (if I correctly understand), you might want to consider using NSData
or Data
, rather than UnsafeMutablePointer<UInt8>
for each image buffer. These types are specifically intended for dealing with an array of bytes, which is what an image buffer is; see
https://developer.apple.com/reference/foundation/nsdata and
https://developer.apple.com/reference/foundation/data
这里是一个人为的例子,说明如何使用 Data
和 NSData
来做到这一点:
Here is a contrived example of how this could be done using Data
and NSData
:
Objective-C 实现:
Objective-C implementation:
@implementation MyObjC
+ (void) call: (NSArray * ) arr {
NSData * data1 = arr[0];
UInt8 * bytes1 = (UInt8 *)data1.bytes;
bytes1[0] = 222;
}
@end
斯威夫特:
var arr: [UnsafeMutablePointer<UInt8>] = []
// This is just an example; I'm sure that actual initialization of someImage is more sophisticated.
var someImage = UnsafeMutablePointer<UInt8>.allocate(capacity: 3)
someImage[0] = 1
someImage[1] = 12
someImage[2] = 123
// Create a Data instance; we need to know the size of the image buffer.
var data = Data(bytesNoCopy: someImage, count: 3, deallocator: .none)
var arrData = [data] // For demonstration purposes, this is just a single element array
MyObjC.call(arrData) // You may need to also pass an array of image buffer sizes.
print("After the call: \(someImage[0])")
这篇关于如何将元素从 Swift 中的 [UnsafeMutablePointer<UInt8>] 转换为 C++ 中的 UInt8 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!