• 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: Create NSStrings from a mapped NSData object - safe?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Create NSStrings from a mapped NSData object - safe?


  • Subject: Re: Create NSStrings from a mapped NSData object - safe?
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 13 May 2008 16:39:36 -0700


On 13 May '08, at 12:55 PM, Daniel Vollmer wrote:

It sounds like you're creating a single NSString containing the entire contents of the file, then?

Yes. Is that something I shouldn't do? I mean, I feel a tiny bit silly creating such huge strings but I didn't find a nice alternative (e.g. like the Ruby for each line iterators on file objects).

Unfortunately streams are not Foundation's strong suit. You can use NSStream or NSFileHandle to read incrementally from a file, but the API's pretty low-level and you'll have to do things like decoding UTF-8 and parsing for line ends by yourself.


But now that means that the strings are "endangered" from in-place file modification for the lifetime of my objects created during parsing, not just the initial parsing itself, correct?

The big 20MB string might be, yes. If you created any new NSStrings as substrings of it, I am pretty sure those have their own copies of the character data, so they should be immune.


Note that even if you used a stream to read the file incrementally, you wouldn't be immune to something else modifying the file while you were reading it. So the effect isn't all that different. Just be sure to release and stop using the big 20MB string right after you finish scanning it.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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

References: 
 >Create NSStrings from a mapped NSData object - safe? (From: Daniel Vollmer <email@hidden>)
 >Re: Create NSStrings from a mapped NSData object - safe? (From: Jens Alfke <email@hidden>)
 >Re: Create NSStrings from a mapped NSData object - safe? (From: Daniel Vollmer <email@hidden>)

  • Prev by Date: Re: NSSearchField in menu item weirdness
  • Next by Date: Re: Anybody using Pantomime or mail-core framework?
  • Previous by thread: Re: Create NSStrings from a mapped NSData object - safe?
  • Next by thread: Re: Create NSStrings from a mapped NSData object - safe?
  • Index(es):
    • Date
    • Thread