Working with NSUserDefaults Object

The NSUserDefaults object is used to store data values for an app in a key-value list on the disk. Working with NSUserDefaults is very simple. You start by getting a reference to the standard NSUserDefaults object with this line of code:

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];

To store a value in the settings object, you use code like this:

[settings setObject:@"City" forKey:@"sortField"];

This saves the value “City” with the key “sortField”. Every value must have a unique key. In addition to the setObject:forKey: method, there are also methods that can be used to store scalar values such as BOOL and int. Here’s an example: ...

Get Learning Mobile App Development: A Hands-on Guide to Building Apps with iOS and Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.