Loading and Saving

There are several different persistence mechanisms in iOS. The simplest to use is a persistent key-value store called NSUserDefaults. You might use NSUserDefaults for similar purposes as cookies in web development. They can store things like application settings, the current user, or a flag for whether a user has already seen a helpful hints popover.

To save a key to NSUserDefaults, do something like this:

var defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("some_string_to_save", forKey: "some_key_that_you_choose")
defaults.setInteger(123, forKey: "another_key_that_you_choose")
defaults.synchronize()

To load a key from NSUserDefaults, do something like this:

var defaults = NSUserDefaults.standardUserDefaults()
var stringValue = defaults.objectForKey("some_key_that_you_choose") as String
var intValue = defaults.integerForKey("another_key_that_you_choose")

Note the synchronize call. NSUserDefaults automatically and periodically synchronizes, but to manually flush the keys and values to disk, call synchronize to guarantee that your updates are saved.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s