轉貼自
1. http://rintarou.dyndns.org/tag/objective-c/2. developer.apple.com/library
3. 完整的例子可以參考http://blog.skp.idv.tw/kun/
Archive 與 Serialization 是為了將 Hierarchical data 與 byte stream 相互轉換(encode/decode)而設計的兩種機制。
Encode : Hierarchical data -> byte stream
Decode : byte stream -> Hierarchical data
Archives (Un-arhchive)
Archive 能處理包含 Object 本身,以及與其它 Object 之間的 Relationships (references),保存原來完整 Object graph 的資訊並轉換成 byte stream,例如:nib。要支援 Archive 機制,就必須實作 NSCoding Protocol,在 Fundation 裡的一些 Value objects(NSString, NSArray, NSNumber 等)都有實作 NSCoding Protocol。
Mac OS X archives store an arbitrarily complex object graph. The archive preserves the identity of every object in the graph and all the relationships it has with all the other objects in the graph. When unarchived, the rebuilt object graph should, with few exceptions, be an exact copy of the original object graph.
Interface Builder uses archives (nib file) to store the objects and relationships that make up a user interface. A Cocoa application loads the nib archive to reconstruct a window, menu, or view that was designed in Interface Builder.
Your application can use an archive as the storage medium of your data model. Instead of designing (and maintaining) a special file format for your data, you can leverage Cocoa’s archiving infrastructure and store the objects directly into an archive. With minimal effort, you can implement Save and Open in your application.
To support archiving, an object must implement the NSCoding protocol, which consists of two methods. One method encodes the object’s important instance variables into the archive and the other decodes and restores the instance variables from the archive.
All of the Foundation value objects (NSString, NSArray, NSNumber, and so on) and most of the Application Kit user interface objects implement NSCoding and can be put into an archive. Each class’s reference document identifies whether they implement NSCoding.
Mac OS X serializations store a simple hierarchy of value objects, such as dictionaries, arrays, strings, and binary data. The serialization only preserves the values of the objects and their position in the hierarchy. Multiple references to the same value object might result in multiple objects when deserialized. The mutability of the objects is not maintained.
Property lists are examples of serializations. Application attributes (the
Info.plist file) and user preferences are stored as property lists.
Arbitrary objects cannot be serialized. Only instances of NSArray, NSDictionary, NSString, NSDate, NSNumber, and NSData (and some of their subclasses) can be serialized. The contents of array and dictionary objects must also contain only objects of these few classes.
Class hierarchy for coders


