简体   繁体   English

如何从[UnsafeMutablePointer投射元素 <UInt8> 在Swift中转为UInt8 *在C ++中

[英]How to cast element from [UnsafeMutablePointer<UInt8>] in Swift to UInt8 * in C++

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. 我有以下无法编译的代码,因为XCode不允许我将NSArray元素转换为C ++代码中的指针。 The error given by XCode is: Assigning to 'UInt8 *' from incompatible type 'id' . XCode给出的错误是: Assigning to 'UInt8 *' from incompatible type 'id'

How am I supposed to pass an array of type [UnsafeMutablePointer<UInt8>] from Swift to Objective-C++ ? 我应该如何将类型为[UnsafeMutablePointer<UInt8>]的数组从Swift传递给Objective-C ++?

Thank you in advance 先感谢您

objcfunc.h objcfunc.h

+ (void) call: (NSArray *) arr;

objcfunc.mm 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
}

Swift 迅速

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 objcfunc.h

+ (void) callSingle: (UInt8 *) buf;

objcfunc.mm 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);

NSArray is an array of Objective-C objects. NSArray是Objective-C对象的数组。 So, you need to pass an array of instances of types that are bridged to Objective-C types. 因此,您需要传递一系列桥接到Objective-C类型的类型的实例。 I'm not sure that Swift's UnsafeMutablePointer struct is bridged. 我不确定Swift的UnsafeMutablePointer结构是否桥接。

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. 因为在这种情况下,您正在传递图像缓冲区数组(如果我正确理解的话),那么您可能需要考虑对每个图像缓冲区使用NSDataData ,而不是UnsafeMutablePointer<UInt8> 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 请参阅https://developer.apple.com/reference/foundation/nsdatahttps://developer.apple.com/reference/foundation/data

Here is a contrived example of how this could be done using Data and NSData : 这是一个使用DataNSData如何完成此操作的人为示例:

Objective-C implementation: 目标C的实现:

@implementation MyObjC

+ (void) call: (NSArray * ) arr {
    NSData * data1 = arr[0];
    UInt8 * bytes1 = (UInt8 *)data1.bytes;
    bytes1[0] = 222;
}

@end

Swift: 迅速:

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])")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM