SpokaneDude
SpokaneDude

Reputation: 4974

NSUserDefaults not being set

This is my code in AppDelegate.m -didFinishLaunchingWithOptions:

//  set color scheme
SingletonColorScheme *colorScheme = [SingletonColorScheme sharedColorScheme];  //  initialize

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if(colorScheme.colorScheme == nil)
    [defaults setObject:@"Saori" forKey:@"colorScheme"];
else
    [defaults setObject:colorScheme.colorScheme forKey:@"colorScheme"];

[defaults synchronize];  //  write them to disk

colorScheme.colorScheme = [defaults objectForKey:@"colorScheme"];  //  set the singleton

NSLog(@"\n\nAppDelegate - colorScheme: %@\ndefault: %@\n\n", colorScheme.colorScheme, [[NSUserDefaults standardUserDefaults] stringForKey:@"colorScheme"]);

The NSLog shows this:

AppDelegate - colorScheme: 
default: (null)

I know now that the code is not correct, but for the life of me, I don't see what's wrong. I would appreciate some help fixing this! :D

SD UPDATED: changed key for NSLog

Upvotes: 0

Views: 75

Answers (1)

user529758
user529758

Reputation:

stringForKey:@"Saori"

should be

stringForKey:@"colorScheme"

-- you want to get the object for the key that you used previously.

Upvotes: 1

Related Questions