• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Deleting data from NSMutableData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Deleting data from NSMutableData


  • Subject: Re: Deleting data from NSMutableData
  • From: Andy Lee <email@hidden>
  • Date: Wed, 21 Mar 2007 01:25:29 -0400

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

You could add one in a category.

On Mar 20, 2007, at 11:51 PM, Jim Correia wrote:
Also see "Working With Mutable Binary Data":

<http://developer.apple.com/documentation/Cocoa/Conceptual/ BinaryData/Tasks/WorkingMutableData.html#//apple_ref/doc/uid/20002150>

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

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Deleting data from NSMutableData
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >Deleting data from NSMutableData (From: "R. Tyler Ballance" <email@hidden>)
 >Re: Deleting data from NSMutableData (From: Jim Correia <email@hidden>)
 >Re: Deleting data from NSMutableData (From: "R. Tyler Ballance" <email@hidden>)

  • Prev by Date: Re: Embedding a WebView in a Table
  • Next by Date: Re: Deleting data from NSMutableData
  • Previous by thread: Re: Deleting data from NSMutableData
  • Next by thread: Re: Deleting data from NSMutableData
  • Index(es):
    • Date
    • Thread