I am trying to find out how to save/store my values from NSDefaults so that when I exit the application they are stored in the Settings.bundle. This is what I am doing…
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:@"M1", @"IDMissiles",
@"G2", @"IDGuns",
@"B3", @"IDBombs",
@"KM", @"IDDistance", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:settings];
If I do the following, the values print out correctly from NSUserDefaults …
NSLog(@"IDMissiles: %@", [userDefaults stringForKey:@"IDMissiles"]);
NSLog(@"IDGuns : %@", [userDefaults stringForKey:@"IDGuns"]);
NSLog(@"IDBombs : %@", [userDefaults stringForKey:@"IDBombs"]);
NSLog(@"IDDistance: %@", [userDefaults stringForKey:@"IDDistance"]);
However … Each time I run the application the values in NSUserDefaults start off as (null), I was thinking that doing [[NSUserDefaults standardUserDefaults] synchronize]; would store the values for the next time I run the application, but no such luck.
One thing I discovered when working with the settings.bundle is that none of the values get initialized until you actually open the settings pane. You can have default values saved there, but they will return nil until you open the settings.
I’m not sure if this happens when you try and save values there but never open the settings pane.
If you are not using a settings pane, then you wouldn’t want to use the registerDefaults option.
Try this instead.