ios - Objective-C 如何将 NSArray 转换为 Byte 数组?
问题描述
我对objective-c不是很有经验,我需要为以下用例提供建议:
我正在为 ios 编写一个 React 本机模块,并且 ios 端的本机方法之一接收一个NSArray
包含整数(每个整数的值介于 0 和 255 之间)。
在我的 Objective-C 代码中,我需要将其转换NSArray
为 aByte 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[]
推荐阅读
- django - Django聚合问题
- shell - Elisp - 如何运行 shell 命令并使缓冲区处于降价模式?
- android - 使用 SharedPreferences 获取月份中的日期
- javascript - Javascript本地存储条件没有响应
- foreach - 在 BI Publisher 中打印同一变量的多个实例
- javascript - bootstrap-vuejs 分页导航组件
- javascript - 如何在页面之间共享 JS 变量?
- javascript - Firestore 中可以使用多少个集合文档对?
- python - 如何过滤嵌套列集中的熊猫数据框?
- python - Raspberry Pi 上 Python 脚本作为服务的问题