Daniel Thompson
Daniel Thompson

Reputation: 2353

iOS -- NSCoding -- storing data in PList vs Documents Directory

Why would I store encoded data in a plist when I can just store it in a structured dictionary?

For example, in Apple's learning materials they do this:

“let notes = [note1, note2, note3]
 
let archivedNotes = NSKeyedArchiver.archiveRootObject(notes,
toFile: archiveURL.path)”

Essentially just tossing a piece of data in some path.

But I've seen people using Plist files to store NSCoded data like this. What are the advantages of one strategy over the other? Is there a best practice for working with stored data that anyone can direct me toward?

Plists seem to give you some value with the data having keys (punn not intended), but couldn't you just make a dictionary with the keys to your various pieces of data?

Upvotes: 1

Views: 358

Answers (1)

Rob
Rob

Reputation: 437392

See Archives and Serializations Programming Guide: Serializing Property Lists for a discussion of why, when you're capturing a hierarchical object graph, why "coding (as implemented by NSCoder and its subclasses) is the preferred way to make object graphs persistent":

Serialization converts Objective-C types to and from an architecture-independent byte stream. In contrast to archiving, basic serialization does not record the data type of the values nor the relationships between them; only the values themselves are recorded. It is your responsibility to deserialize the data in the proper order.

Property list serialization does not preserve the full class identity of the objects, only its general kind—a dictionary, an array, and so on. As a result, if a property list is serialized and then deserialized, the objects in the resulting property list might not be of the same class as the objects in the original property list. In particular, when a property list is serialized, the mutability of the container objects (NSDictionary and NSArray objects) is not preserved. When deserializing, though, you can choose to have all container objects created mutable or immutable.

Serialization also does not track the presence of objects referenced multiple times. Each reference to an object within the property list is serialized separately, resulting in multiple instances when deserialized.

Because serialization does not preserve class information or mutability, nor handles multiple references, coding (as implemented by NSCoder and its subclasses) is the preferred way to make object graphs persistent.

Bottom line, you'd generally prefer to use NSKeyedArchiver (or in Swift 4, PropertyListEncoder) because it captures information about the classes that are encoded and can capture richer types.

Having said that, plists are very simple and are often rendered in text formats that make it easy to visually inspect the resulting data and you can see what was captured. And often, if you're dealing with a nice simple list, plists are a nice, slightly simplified solution.

Upvotes: 1

Related Questions