• 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: NSMutableData not playing ball
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableData not playing ball


  • Subject: Re: NSMutableData not playing ball
  • From: email@hidden
  • Date: Wed, 13 Feb 2002 18:59:02 -0800

The original message appears to have been sent to macosx-dev and bcc:ed to cocoa-dev, so I'm replying to both lists. Sorry for the cross-posting.

I have a data object with some bytes in it, such as:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

and I want to replace a range of characters, say { 6, 5 } with an NSString,
say @"XYZ", thus producing the output:

ABCDEXYZKLMNOPQRSTUVWXYZ

so that now the letters FGHIJ have been replaced by XYZ. My problem is
that -replaceBytesInRange:withBytes: has no idea how large the string I'm
passing in is, so just replaces five bytes with five bytes, giving XYZ and
two random bytes, whatever happened to be next in memory. I could do with
a -replaceBytesInRange:withData: method!

How am i supposed to do this?

(note I am doing the NSString -> char* thing, but left it out of the
description for simplicity)

The documentation for replaceBytesInRange:withBytes: actually asserts that it can do exactly what you want ("The receiver is resized to accommodate the new bytes, if necessary."), and there is sample code there showing how to do it (turning "Liz and John" into "Liz and Larry"). The doc is not explicit about this, but it appears that this method expects the "withBytes:" parameter to be a malloced block, and asks the allocator for the size of the block malloced.
This seems rather questionable API design to me, as it assumes the pointer passed in is malloced, assumes it knows which allocator to ask about the block, etc. So perhaps the doc is wrong, but it's pretty clear in what it states the behavior to be. Have you tried using it in the way the doc suggests?

Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSMutableData not playing ball
      • From: Marcel Weiher <email@hidden>
  • Prev by Date: Re: design problem
  • Next by Date: Re: another IB bug
  • Previous by thread: NSMutableData not playing ball
  • Next by thread: Re: NSMutableData not playing ball
  • Index(es):
    • Date
    • Thread