我有以下代码不会编译,因为XCode不会让我在我的C++代码中将一个NSArray元素转换为指针。 XCode给出的错误是:Assigning to 'UInt8 *' from incompatible type 'id'
。如何将元素从Swift中的[UnsafeMutablePointer <UInt8>]转换为C++中的UInt8 *
我该如何将类型[UnsafeMutablePointer<UInt8>]
的数组从Swift传递到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)
但是,如果我不使用数组和直接传递指针,代码工作正常:
objcfunc.h
+ (void) callSingle: (UInt8 *) buf;
objcfunc.mm
+(void) callSingle: (UInt8 *) buf {
unsigned char *image;
image = (unsigned char *) buf; // works fine
processImage(image);
}
Swift
let x: UnsafeMutablePointer<UInt8> buf;
// initialize buf
objcfunc.callSingle(buf);