开发者

Problem with NSUserDefaults and deallocated Instance

开发者 https://www.devze.com 2022-12-22 17:56 出处:网络
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'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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消