我有以下代碼不會編譯,因爲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);