ios - Objective-C 如何将 NSArray 转换为字节数组?

标签 ios objective-c arrays byte nsarray

我对 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/

相关文章:

IOS 视频播放器示例

android - 智能手机浏览器上的 SVG 支持

c - 如何检查位图数组中是否至少有一个元素不为零?

objective-c - 调用 UIActionSheet 时应用程序崩溃

javascript - for 循环将多个值推送到数组中

c - 为什么结构数组允许我索引到边界数组之外

ios - Google Play 服务 - Unity - 在 iOS 上只能邀请 'recent players'

ios - 为什么我不能使用 prepareForSegue 传递值?

ios - 在 xCode obj 中实现滑动手势。 C

iOS 蓝牙 : Unknown error when writing to characteristic