Nick Lockwood

Nick Lockwood is the author of iOS Core Animation: Advanced Techniques. Nick also wrote iCarousel, iRate and other Mac and iOS open source projects.

Object Serialization using NSCoder and NSCoding

One of the neat features of Cocoa is how easy it makes it to serialize objects to various standard formats for storing in a file, or transmission over The Internet.

We’ve covered how to serialize objects to Property Lists (AKA ‘Plists’) and JSON previously, but by default only a small set of standard object types can be saved in this way (e.g. NSDictionary, NSArray, NSString, etc.)

What if you want to save a custom object type; one that you have created yourself? In theory you can write methods to convert your own objects to and from dictionaries or strings, but this is cumbersome, and there are lots of nasty edge cases to think about, such as circular references. There is a better way:

Continue reading