O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

What Can Be Stored in NSUserDefaults?

You may have noticed that, aside from the scalar types, NSUserDefaults provides APIs for setting and getting objects. What kind of objects? Property list objects.

Property list objects are instances of the Objective-C classes NSDictionary, NSArray, NSString, and NSNumber. A plist data structure is composed entirely of these objects. As such you can store any data structure in NSUserDefaults that you like, as long as you can represent it as a plist. Since the Swift basic types are bridged to these types, you can store them without any issues. An example:

l​e​t​ ​u​s​e​r​D​e​f​a​u​l​t​s​ ​=​ ​N​S​U​s​e​r​D​e​f​a​u​l​t​s​.​s​t​a​n​d​a​r​d​U​s​e​r​D​e​f​a​u​l​t​s​(​)​ l​e​t​ ​p​l​i​s​t​K​e​y​ ​=​ ​"​p​l​i​s​t​"​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required