Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Storing data with NSUserDefaults

Last updated on June 25, 2014

How can you store data between application launches?

There are plenty of ways, but here I will show you the simplest, NSUserDefaults.

NSUserDefaults is a simple property list (aka plist) where an app can store simple data.  While there is no limit to its size (besides the device’s own limits), you should not store a large amount of data here.  The file is written and read atomically (as a whole), so the more data that is in there, the longer that will take.  Nonetheless, it is a great place to store settings, high scores, and the like.

Intro to Property Lists

Property lists can only accept certain types of variables, so NSUserDefaults has that limitation.  You can store these types of variables:

  • NSArray
  • NSData
  • NSDictionary
  • NSNumber
  • NSString

Furthermore, the NSArray or NSDictionary must only contain the types listed above (yes, nesting of NSArray or NSDictionary is allowed).  Other items that conform to the NSCoding protocol can be archived as NSData, so you can store them in property lists if necessary.  I have not really looked in to NSCoding yet, so that will be the subject of some future post.  Also, the keys for the root of the property list, as well as any NSDictionary stored inside, must be NSStrings.

Saving to NSUserDefaults

Basically, all you have to do is load NSUserDefaults, and then tell it to save a value to a specific key.  Here is a simple example of writing an integer to NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:9001 forKey:@"HighScore"];
[defaults synchronize];

As mentioned earlier, we loaded the NSUserDefaults plist to the object “defaults”.  We then told that object to set an integer for the key “HighScore”.  That final command to defaults to “synchronize” is basically to force a save.  It isn’t absolutely necessary, because this is called automatically every so often, but if you want to be sure to save the changes to NSUserDefaults right now, this is how.

Pretty simple, eh?

Reading from NSUserDefaults

Reading is even easier, as shown below:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger theHighScore = [defaults integerForKey:@"HighScore"];

That’s all there is to it.  You just load NSUserDefaults, then tell it to retrieve a type for a specific key.  No need to synchronize or anything here, so 1 less line of code than saving.

One extra little tidbit here, you will notice that NSInteger has no asterisk after the type like NSUserDefaults does.  That is because NSInteger is actually just a typecast of long or int, depending on whether the system it is running on is 64-bit or 32-bit respectively, and not a normal Objective-C object.

Conclusion

You can read more on how to store different values in the documentation from Apple, but this is enough to get you started.  If you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do. Thanks!

 

Filed Under: Syntax

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress