• 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: File processing with Obj-C & Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File processing with Obj-C & Cocoa


  • Subject: Re: File processing with Obj-C & Cocoa
  • From: Randall Meadows <email@hidden>
  • Date: Fri, 15 Feb 2008 18:49:34 -0700

On Feb 15, 2008, at 6:22 PM, Bryan Robinson wrote:

I'm a long time C++ developer who has recently started with Obj-C and
Cocoa.  I love it so far, but I have come up against an issue that has
stumped me: how do I processes a structured text file (utf-8) with the
Cocoa and Obj-C?

In C++ I would use the fstream library and then call the getline()
function to read a line into a buffer, and then I would covert the
buffer to a STL string and use the string class methods to parse the
line of text.

Over at ADC, I've looked around at the following classes:
NSInputStream, NSFileHandle and also NSData.  I don't really see any
methods for reading line by line or reading a fixed number of bytes
based on a delimeter.  This leads me to believe that either I've not
discovered the proper class yet, or there is a fundamentally different
way of processing simple text files.  For the time being I'm mixing
C++ and Obj-C, but I would really like to learn the Obj-C/Cocoa way.

Well, you could use one of NSString's -initWithContentsOfFile variants, then -componentsSeparatedBy... to get an array (NSArray) of "lines" (passing the latter the appropriate EOL character(s)), then process each array component as necessary.


Also related, what is the preferred method for storing information to
a file with Cocoa?  Should I use NSArchiver/NSUnarchiver, or should I
write the data to a file in an XML format?

Well, I'd say it depends on what your data is, but again, NSString has methods for writing string contents to a file.


Perhaps NSString is the as-yet-undiscovered "proper class"? :)
_______________________________________________

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: 
 >File processing with Obj-C & Cocoa (From: "Bryan Robinson" <email@hidden>)

  • Prev by Date: Re: NSTextTab at the rightmost edge of view
  • Next by Date: Re: folder content as model for NSTableView
  • Previous by thread: File processing with Obj-C & Cocoa
  • Next by thread: Re: File processing with Obj-C & Cocoa
  • Index(es):
    • Date
    • Thread