Plists vs. NSData


I’m just curious about the use of .plists (page 172) vs. the use of NSData (page 204) to save a file. Are they essentially the same thing? I see the saved file with the .tdl extension opens directly in the native app, which makes sense.


While they’re very similar in terms of what you put into them and what you get out, the approaches are slightly different. The -writeToFile:/-initWithContentsOfFile: family of methods is the easiest and requires the least amount of code, but is fairly limited in terms of the options available.

For example, using the Plist Serialization methods, you can specify whether you’d like your saved file to use the XML or binary plist format (binary is not human readable, but is better for performance.

Additionally, the Plist Serialization methods allow you to specify the mutability of objects inside the object tree that you’re loading from disk. Any objects loaded from disk using the -initWithContentsOfFile: methods will be immutable (NSArrays, NSStrings, etc.), even if they were mutable (NSMutableArray, NSMutableString, etc.) when they were saved. In contrast, The Plist Serialization methods allow you to specify that you’d like containers to be mutable when loaded from disk. Note that the exercise takes advantage of this.

Hope that helps,