1

My code is as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], @"show_outer_reference_circle_preference",
                                 [NSNumber numberWithBool:YES], @"show_seconds_circle_preference",
                                 nil];

    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
    [prefs registerDefaults:appDefaults];

    [glView startAnimation];
    return YES;
}

and further down I poll NSUserDefaults with code as follows:

    NSUserDefaults * userDefaults = [[NSUserDefaults standardUserDefaults] retain]; 
    NSNumber* optionsBoolValue = [userDefaults objectForKey:@"show_outer_reference_circle_preference"];
    NSAssert(optionsBoolValue != nil, @"AAAAHH");
    //...
    [userDefaults release];

And the settings don't come through. As you can see, I set the value to YES, buuuuttt the setting comes through as nil and the app goes like "AAAAHH".

What am I doing wrong?

Thanks in advance,

-Nick

1

1 Answer 1

1

The first part of the code is, as you can see, right after didFinishLaunching. However, the "read-out" part of the code, the second block in the question, is in the init method of an object that is created as a result from loading a nib, and therefore is executed before didFinishLaunching, therefore going like "AAAAHHH".

To correct this, I created an object with one static method for setting the userDefaults, and calling that right before I read any setting.

3
  • Using Cocoa Binding will always help you get the read out correct, regardless you register defaults sooner or later. (Although you should really do it sooner.) Your controller which binds its keys to the keys of [NSUserDefaultsController sharedUserDefaultsController] will always get notified of any changes to the defaults.
    – koo
    Dec 11, 2010 at 16:52
  • 1
    Is NSUserDefaultsController available under iOS? I haven't been able to find it in the API.
    – Will Baker
    Jan 28, 2011 at 3:59
  • 1
    @Will Baker, I don't think so. I think it's only part of AppKit, which I think is for Mac apps, not iOS apps. I think this is the SAT analogy: AppKit : Cocoa :: UIKit : Cocoa Touch
    – ma11hew28
    May 8, 2011 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.