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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access