[英]Insert bytes at front in NSMutableData
How can I insert a byte at the beginning of my NSMutableData
? 如何在
NSMutableData
的开头插入一个字节? I understand that there is a replaceBytesInRange:
method but that will just replace the bytes. 我知道有一个
replaceBytesInRange:
方法,但这只会替换字节。 There is a bunch of insertXAtIndex:
methods but none are for bytes. 有一堆
insertXAtIndex:
方法,但没有一个是字节。 How can I do this? 我怎样才能做到这一点? One way I can think of doing this is:
我能想到这样做的一种方法是:
NSMutableData *theData = [NSMutableData dataWithBytes:myByte];
[theData appendData:myOriginalData];
myOriginalData = nil;
But there must be a better way. 但必须有更好的方法。
I also tried this but it didn't work: 我也试过这个,但它不起作用:
char *sec = "Second!";
char *fir = "First!";
NSMutableData *theData = [NSMutableData dataWithBytes:(const void *)sec length:7];
[theData replaceBytesInRange:NSMakeRange(0, 0) withBytes:(const void *)fir];
NSString *str1 = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str1); //Prints "Second!"
[theData replaceBytesInRange:NSMakeRange(0, 0) withBytes:(const void *)fir length:strlen(fir)];
应该做的伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.