【发布时间】:2012-09-28 15:55:34
【问题描述】:
如何在 NSMutableData 对象中设置一个字节? 我尝试了以下方法:
-(void)setFirstValue:(Byte)v{
[mValues mutableBytes][0] = v;
}
但这会让编译器大声哭泣......
【问题讨论】:
标签: objective-c arrays byte nsmutabledata
如何在 NSMutableData 对象中设置一个字节? 我尝试了以下方法:
-(void)setFirstValue:(Byte)v{
[mValues mutableBytes][0] = v;
}
但这会让编译器大声哭泣......
【问题讨论】:
标签: objective-c arrays byte nsmutabledata
但这会让编译器大声哭泣......
那是因为mutableBytes* 返回void*。将其投射到char* 以解决问题:
((char*)[mValues mutableBytes])[0] = v;
你也可以使用replaceBytesInRange:withBytes:
char buf[1];
buf[0] = v;
[mValues replaceBytesInRange:NSMakeRange(0, 1) withBytes:buf];
【讨论】:
我把它转换成一个数组
NSMutableData * rawData = [[NSMutableData alloc] initWithData:data];
NSMutableArray * networkBuffer = [[NSMutableArray alloc]init];
const uint8_t *bytes = [self.rawData bytes];
//cycle through data and place it in the network buffer
for (int i =0; i < [data length]; i++)
{
[networkBuffer addObject:[NSString stringWithFormat:@"%02X", bytes[i]]];
}
那么你当然可以调整你的networkBuffer中的对象(这是一个nsmutablearray)
【讨论】: