我对 objective-c 不是很有经验,我需要为以下用例提供建议:
我正在为 ios 编写一个 React native 模块,并且 ios 端的一个 native 方法接收一个包含整数的 NSArray
(每个整数的值介于 0 和 255 之间)。
在我的 objective-c 代码中,我需要将此 NSArray
转换为 Byte bytes[]
,然后将其传递给我使用的某些 SDK。
我该如何进行转换?
最佳答案
NSArray* array = @[@(1), @(5), @(115), @(34)];
Byte* bytes = calloc(array.count, sizeof(Byte));
[array enumerateObjectsUsingBlock:^(NSNumber* number, NSUInteger index, BOOL* stop){
bytes[index] = number.integerValue;
}];
doWhateverWithBytes(bytes);
free(bytes);
假设 doWhateverWithBytes
接受 Byte bytes[]
关于ios - Objective-C 如何将 NSArray 转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54344010/