1

如何在 NSMutableData 对象中设置一个字节?我尝试了以下方法:

-(void)setFirstValue:(Byte)v{
    [mValues mutableBytes][0] = v;
}

但这会让编译器大声喊叫......

4

2 回答 2

5

但这会让编译器大声喊叫......

那是因为mutableBytes*返回void*. 将其转换char*为解决问题:

((char*)[mValues mutableBytes])[0] = v;

你也可以使用replaceBytesInRange:withBytes:

char buf[1];
buf[0] = v;
[mValues replaceBytesInRange:NSMakeRange(0, 1) withBytes:buf];
于 2012-09-28T15:58:45.227 回答
1

我把它转换成一个数组

    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)

于 2012-09-28T15:58:19.140 回答