How to use NSUserDefaults for save/load data on iOS

This will be showed in Objective-C. NSUserDefaults is the easiest way to save/load data on iOS. By the easiest I mean that you need least code to write.

Code example for saving is:

 // singleton instance of NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// setting value for key
[defaults setValue:@”My Data” forKey:@”infoString”];

// this will do the save
[defaults synchronize];

Code example for loading is:


// singleton instance of NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// geting value for keyNSString *myString = [defaults objectForKey:@”infoString”];

In next post I will explain for what data to use NSUserDefaults , and for what data never to use it.

Show your support

Clapping shows how much you appreciated WebOrCode’s story.