【问题标题】:Byte array in objective-cObjective-C中的字节数组
【发布时间】:2009-05-18 07:36:14
【问题描述】:

如何在 Objective-C 中创建字节数组?

我有一些像这样的数据:0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89,我想准备一个字节数组。

【问题讨论】:

    标签: objective-c arrays byte


    【解决方案1】:

    您可以简单地使用 Objective-C 中提供的纯 C 语法:

    const char myByteArray[] = {
        0x12,0x23,0x34,0x45,
        0x56,0x67,0x78,0x89,
        0x12,0x23,0x34,0x45,
        0x56,0x67,0x78,0x89 };
    

    【讨论】:

    • 非常感谢……亲爱的 mouviciel,它工作正常。
    • 如果使用无符号字符有什么区别?
    • 我喜欢 C 字节数组,但是如果你想在 NSArray 中使用多个字节数组会发生什么? Implicit conversion of a non-Objective-C pointer type 'const char *' to 'id _Nonnull' is disallowed with ARC。我喜欢下面来自@Georg 的回答者。 NSData *data = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)]; ?
    【解决方案2】:

    要么使用 mouviciel 给出的纯 C 解决方案,要么阅读 NSDataNSMutableData 的类描述。

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多