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​"​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide 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.