Re: File processing with Obj-C & Cocoa
Re: File processing with Obj-C & Cocoa
- Subject: Re: File processing with Obj-C & Cocoa
- From: John Stiles <email@hidden>
- Date: Fri, 15 Feb 2008 18:18:36 -0800
Do you control the format of the file? If so, you can put all your data
in a container like an NSArray or NSDictionary and then there are
one-liner functions to save and load them to a plist
(-writeToFile:atomically: etc.). This is a very convenient way to go.
For line-by-line processing of a file, if you don't control the file
format, you could read in the whole file—NSString lets you specify the
encoding—and then you can use various NSString methods to break it up
into an array of lines (-componentsSeparatedByString: etc). This
shouldn't be more than a few lines of code.
Unichar's "type" is a UTF-16 codepoint, and the NSString API focuses on
unichars. But NSString can load almost any encoding type with no extra
work on your part. (And internally it does not always use unichars, e.g.
for ASCII it can save space and only use a byte—this is all hidden from
you, it's just an implementation detail for performance.)
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.
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?
And also related, on OS X, what is the internal representation for
NSString and NSMutableString? On the NSString reference page, it says
data is stored in Unicode, but it doesn't say which "type" is used.
Is it UCS-4? Or UCS-2 like Windows? I've been impressed at the ease
in which I can use C++ to process UTF-8 files, and I am just curious
about things on the Obj-C/Cocoa side.
Thank you for your time and help.
_______________________________________________
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
_______________________________________________
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