Both return the same pointer. I know - bytes belongs to NSData, why does NSMutableData introduce - mutableBytes? Is it just for code clarity so it is more obvious you are accessing mutable data? Does it really matter which one is used?
NSMutableData* mydata = [[NSMutableData alloc] init];
[mydata appendData: [@"hello" dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%p", [mydata mutableBytes]);
NSLog(@"%p", [mydata bytes]);
Thanks.
There are a couple of reasons why
NSMutableDatamight provide a separatemutableBytesmethod:As you suggested in your question, using
mutableBytesmakes it clear to the reader that you want to change the data.The
bytesmethod returns aconst void *. ThemutableBytesmethod returns avoid *. If you want to change the bytes, you need avoid *with noconstqualifier. ThemutableBytesmethod eliminates the need to cast away theconstqualifier.In theory there could be a third reason: the
-[NSData mutableCopy]method could return anNSMutableDatathat points to the same buffer as the originalNSData, and only create a new, mutable copy of the buffer when you callmutableBytes. However, I don’t think it’s implemented this way based on my very limited testing.