Saving Data with NSUserDefaults and NSCoding

Applications generally have long-lasting consequences: we take a picture, create a presentation, or just unlock a new level. iOS will try to keep your app in memory for a reasonable amount of time, but eventually you need to permanently save something to the disk. There are several ways of doing this, ranging from writing files to using a SQLite database. We’re going to use something in the middle: NSUserDefaults.

NSUserDefaults lets us persist basic objects (strings, numbers, arrays, and hashes) through a simple key-value interface. It handles the serialization mechanics for us, saving you the trouble of constructing a custom file serialization scheme. But it has one more trick up its sleeve: coupled ...

Get RubyMotion now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.