On Mar 21, 2007, at 12:04 AM, R. Tyler Ballance wrote:
I had thought about seeing what I could crash with using
replaceBytesInRange:withBytes:length: and passing it a NULL, but
that seems hackish and unstable at best.
I was going to suggest a combination of -mutableBytes, strncpy(), and
-setLength, but this method sounds like exactly what you need. I
haven't tested all boundary cases, but it seems to work. I don't see
any reason not to use it.
I wouldn't pass it NULL, even if it happens to work. I'd pass it a
non-NULL pointer like self, or if that doesn't feel right, [self
bytes]. It shouldn't matter, since you'll pass 0 for the length.
There's not exactly a removeBytesInRange: method however
That's helpful, but I didn't like the example code there, so I wrote
my own version of it, which I used to test data removal:
// Create an NSMutableData containing a C string, including the
null terminator.
const char *s = "Kate for Edith";
NSLog(@"initial string is %s", s);
NSMutableData *myData = [NSMutableData dataWithBytes:s length:
(strlen(s) + 1)];
// Do an insertion before the null terminator.
NSRange range = NSMakeRange(strlen(s), 0);
[myData replaceBytesInRange:range withBytes:" too" length:4];
NSLog(@"string after insertion is %s", [myData bytes]);
// Do a deletion in the middle.
range = NSMakeRange(5, 1);
[myData replaceBytesInRange:range withBytes:[myData bytes]
length:0];
NSLog(@"string after deletion is %s", [myData bytes]);
// Do a replacement in the middle.
range = NSMakeRange(5, 2);
[myData replaceBytesInRange:range withBytes:"and" length:3];
NSLog(@"string after replacement is %s", [myData bytes]);
As expected, I got the following output:
2007-03-21 01:13:37.030 Scratch[12961] initial string is Kate for Edith
2007-03-21 01:13:37.030 Scratch[12961] string after insertion is Kate
for Edith too
2007-03-21 01:13:37.030 Scratch[12961] string after deletion is Kate
or Edith too
2007-03-21 01:13:37.030 Scratch[12961] string after replacement is
Kate and Edith too
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com