【问题标题】:Objective-C how to convert a NSArray to Byte array?Objective-C 如何将 NSArray 转换为 Byte 数组?
【发布时间】:2019-06-18 00:47:39
【问题描述】:

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

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

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

我怎样才能进行这种转换?

【问题讨论】:

    标签: ios objective-c arrays byte nsarray


    【解决方案1】:
    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[]

    【讨论】:

    • 感谢您的回答! sizeof(byte)Use of undeclared identifier 'byte'
    • 不应该是数组的大小吗?
    • 数组的大小作为calloc的第一个参数
    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多