首页 > 解决方案 > Objective-C 如何将 NSArray 转换为 Byte 数组?

问题描述

我对objective-c不是很有经验,我需要为以下用例提供建议:

我正在为 ios 编写一个 React 本机模块,并且 ios 端的本机方法之一接收一个NSArray包含整数(每个整数的值介于 0 和 255 之间)。

在我的 Objective-C 代码中,我需要将其转换NSArray为 aByte bytes[]以将其传递给我使用的一些 SDK。

我该如何进行这种转换?

标签: iosobjective-carraysbytensarray

解决方案


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[]


推荐阅读