Why is NSUserDefaults used in iOS?

Amogh Talpallikar picture Amogh Talpallikar · Jan 10, 2012 · Viewed 18.3k times · Source

I am new to iOS development and Mobile app development in general, I have decent knowledge about Objective C and MVC patterns to be used and I have never done any Mac development.

I am struggling to understand what NSUserDefaults is for?

We already have something like PList which stores data as XML and we have SQLite which is lightweight DB for such devices.

Then why do we have it?

Is it an alternative simple key-value storage for our app in the same way we have different types of storage on the cloud like an RDBMS and a key-value based NoSQL store etc ?

For NSUserDefaults, Apple docs say that :-

"Applications record such preferences by assigning values to a set of parameters in a user’s defaults database"

What do they mean by user's default database?

My guess is that, like in any multi-user operating system we have various user accounts and in the same way in Mac as well we might be having multiple users each having a database from where applications would load saved preferences for that user.

So like Mac OS X, does iOS also have multiple users and depending upon whichever is logged in NSUserDefaults picks his/her preferences?

Please tell me if I am wrong.

Answer

davehayden picture davehayden · Feb 23, 2012

One thing that hasn't been mentioned yet: NSUserDefaults supports all basic scalar (float, int, BOOL) types, as well as the plist-compatible types: NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. (You mentioned plists as an alternative--NSUserDefaults is just a front-end to a standard plist file in the application's Preferences folder.) This makes it easy to create persistent storage for your application state in just a few lines of code. Even better, if you implement NSCoding on your model objects, you can archive them into an NSData and put them in NSUserDefaults:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:arrayOfModelObjects];

[[NSUserDefaults standardUserDefaults] setObject:data forKey:kDefaultsKeyModelObjects];

and restoring your app data is as simple as

- (void)viewDidLoad
{
    NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:kDefaultsKeyModelObjects];

    arrayOfModelObjects = [[NSKeyedUnarchiver unarchiveRootObject:data] retain];

    // error checking, tell UI to load the new values, etc...
}

(kDefaultsKeyModelObjects is a string constant you've defined somewhere. Always put your NSUserDefaults keys in one place! Having a typo in a key name can take hours to debug. :)

If you used, e.g., SQLite to store your application data, you have to write a lot of code to move your model data in and out of the database. That makes sense if you're dealing with a lot of data, need efficient searching, etc.; but if it's, say, a list of servers in a networking app, it's easier and cleaner to throw them in NSUserDefaults.

One last thing: NSUserDefaults is great for prototyping. Even if you know you're going to need a SQLite store eventually, you can start out using NSUserDefaults as a quick-and-easy persistent store, and get your UI working and your data model fleshed out before writing a bunch of database code.

So like Mac OS X, does iOS also have multiple users and depending upon whichever is logged in NSUserDefaults picks his/her preferences ?

Nope, on iOS the defaults plist is in the application's Library/Preferences folder, not in a user-specific folder like it is on OS X. I can't imagine iOS will ever have multiple logins, but you never know. If it did, they'd have to make separate defaults plists for different users.