Tuesday, February 13, 2007

Reading and Writing Data using Objective-C

Okay, Reading and writing data was something I made a big deal out of. Mainly because I started out with C# two years ago. In C#, you have to declare a StreamReader or a StreamWriter. After which, you then give it a location of a file and it returns a string based on the current line you are on. This made since once I figured it out. Then I got a mac a year back and wanted to figure out how to do the same in Objective-C. I tried the hardest method first thinking it was the easiest. You would think that you would do the same in Objective-C that you do in C#. Not so! All the work has been done for you!
Most objects which contain a NS at the beginning of the name have a method called writeToFile:(NSString *)aLocation.

And if they contain a writeToFile, they also contain a initWithContentsOfFile:(NSString *)aLocation.

So, with that said, this is how you will read in a file to a String.
NSString *filesContent = [[NSString alloc] initWithContentsOfFile:@"file.txt"];

To write a string to a file, well, That's easy too.

[yourDataString writeToFile:@"file.txt"];

Another thing I found to be the most useful in Objective-C is that when you write an array to a file, it converts it over to XML. If you have an array of objects, it will convert those objects to XML and put them in the list as well. This is great especially if you are creating a Mac and Windows app that you need to have working together. You can have your windows app conform to the mac's xml standard for writing arrays and you have two applications working on two systems that work off the same file format.

It is also wonderful because there is hardly any code needed to make it read and write data.

Apple has done it better again! :-)


Eric said...

It's true a file can easily be read into an NSString, but how do you split the contents to process it line by line after?

A NSScanner could be used and asked to read until a newline, but then we'd have to know which delimiter is used for line ends on this particular file.

Is there a one-liner to do the splitting job?

Ryan said...


A new line is signified by the following string "\r\n". If you want, you can use the NSString method called componentsSeparatedByString. All you have to do is pass in that string, “\r\n”, to that method and it should return a NSArray of all the lines in the string. I could be completely wrong on this so I'll run a test on it to make sure. But I know the string “\r\n” is a universal sequence to represent a new line. I’ll post this up on the blog tomorrow after I test it out.

Todd said...

Good 'ole Apple religious assumptions... They infuriate me.

Its fine to say Apple has done a lot of great things - but the only thing Apple has to do with Objective C is that they offer an IDE that supports it...

and there is no such thing as Apple XML.

XML is an open standard.

Apple is a marketing company, that bases its market research on that statistical reality that people make assumptions like the ones make here. They count on people not doing their homework, and thinking Apple invented stuff first.

Caleb said...
This comment has been removed by the author.
Caleb said...


The NSString (and all NS-prefixed classes) are part of the Cocoa framework which Apple did create. So the author was correct in stating that Apple had done it better, because Apple created the framework this article was talking about.

Secondly, the 'Apple XML' he was referring to was the way Cocoa serializes the object into XML which again, is something awesome that the framework supports, that Apple created.

So... who needs to do their homework?

Greg Lee said...

Actually Apple only purchased the Objective-C neXTSTEP framework for cocoa from neXTSTEP a few decades back, they didn't create the "NS" they bought it

Greg Lee said...
This comment has been removed by the author.