views:

104

answers:

1

Hi,

I'm having a problem with NSUserDefaults. I've followed the steps in the books as closely as I can for my app, but still get the same problem.

I am getting a

*** -[NSUserDefaults integerForKey:]: message sent to deallocated instance 0x3b375a0

error when I try and load in the settings. Here is the code that I have, it is in the App Delegate class.

- (void)applicationDidFinishLaunching:(UIApplication *)application {
       recordingController = [[RecordingTableViewController alloc] initWithStyle:UITableViewStylePlain];
       [recordingController retain];
        // Add the tab bar controller's current view as a subview of the window
        [window addSubview:tabBarController.view];

        [self loadSettings];   
    }

    -(void)loadSettings
    {
       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

       NSNumber loop = [defaults objectForKey:@"loop_preference"];
       NSNumber play = [defaults objectForKey:@"play_me_preference"];
       NSNumber volume = [defaults objectForKey:@"volume_preference"];   
    }

As you can see I am not trying to do anything with the values yet, but I get the error on the line reading in the loop preference. I also get it if I try and read an NSString.

Any suggestions would be greatly appreciated.

Thanks

Peter

+1  A: 

Since NSNumber is an object, it seems likely to me that you want:

   NSNumber *loop = [defaults objectForKey:@"loop_preference"];
   NSNumber *play = [defaults objectForKey:@"play_me_preference"];
   NSNumber *volume = [defaults objectForKey:@"volume_preference"];   

(Add asterisks * after NSNumber and before the variable names.) Though this does not seem directly related to your error message, it's the only apparent quirk in your code.

Isaac
Extra Tip: I'd suggest loading settings in `+(void)initialize` method of your delegate class. It gets called even before the `applicationDidFinishLaunching` delegate method so all your settings are available in that method too.
Eimantas