我有一个 NSData 对象,它应该像字节数组一样工作。
我需要得到1st and 2nd bytes in the NSData
,但不知道如何。
如果我在 Java 中有一个字节数组,我可以很容易地通过 获取它们barray[0] and barray[1]
,但是我该如何为 NSData 做呢?
谢谢
NSData *data = [NSData dataWithBytes:"abc" length:3];
const unsigned char* bytes = [data bytes];
NSLog(@"%c %c",bytes[0],bytes[1]);
您可以使用此代码,
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
现在byteData[0]
可以了。