O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.10. Archiving Objective-C Objects

The Foundation framework provides several ways of retrieving data from files on disk. The right approach for you depends on the nature of your application and your data. Some of these methods are listed here:

  • Programs that need low-level access to files can use the NSFileHandle class to open files and read data. NSFileHandle provides basic support for opening a file and reading a number of bytes from the file; you can pass the resulting data to NSString or NSNumber initialization methods to interpret the data as usable values. The NSStream subclasses, NSInputStream and NSOutputStream, provide low-level support for data that might reside in a number of different places: in the file system, in memory, on the network, and so on.

  • Common Foundation objects know how to write themselves to disk automatically. For example, you can read and write an NSString using the initWithContentsOfFile: and writeToFile:atomically: methods. Other objects that can read and write themselves to disk include NSData, NSArray, and NSDictionary. Note you can use only the NSArray and NSDictionary classes to write out objects that can be found in property lists: NSData, NSDate, NSNumber, NSString, and other NSArray and NSDictionary objects. In fact, the NSDictionary and NSArray classes' writeToFile:atomically: method is normally how you write property lists.

  • You can encode one or more Objective-C objects into a single NSData item, and then write that NSData item to disk using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required