【发布时间】:2017-04-13 00:57:45
【问题描述】:
我有以下无法编译的代码,因为 XCode 不允许我将 NSArray 元素转换为 C++ 代码中的指针。
XCode给出的错误是:Assigning to 'UInt8 *' from incompatible type 'id'.
我应该如何将 [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);
}
斯威夫特
let x: UnsafeMutablePointer<UInt8> buf;
// initialize buf
objcfunc.callSingle(buf);
【问题讨论】:
标签: c++ ios objective-c swift xcode