如何在NSMutableData中设置一个字节

Bor*_*ris 1 arrays byte objective-c nsmutabledata

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

-(void)setFirstValue:(Byte)v{
    [mValues mutableBytes][0] = v;
}
Run Code Online (Sandbox Code Playgroud)

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

das*_*ght 5

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

那是因为mutableBytes*回报void*.将其转换char*为修复问题:

((char*)[mValues mutableBytes])[0] = v;
Run Code Online (Sandbox Code Playgroud)

你也可以用 replaceBytesInRange:withBytes:

char buf[1];
buf[0] = v;
[mValues replaceBytesInRange:NSMakeRange(0, 1) withBytes:buf];
Run Code Online (Sandbox Code Playgroud)