开发者

NSUserDefaults standardUserDefaults not returning results

I created a settings.bundle and added a few items. Now I am trying to access their values from my application.

I am using the standard Apple example:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [self setShouldPlaySounds:[defaults boolForKey:@"play_sounds_preference"]]; 
    // Finish app initialization...
}

I have changed play_sound_preferences to my identifier within the bundle. When my application runs, there is no error being returned, but the value is 0, even though I have set the value to 10. This is confirmed: When I view the settings, the slider is at the 10% mark.

Am I missing something?

Here is the actual code block:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
开发者_开发百科{
    // Override point for customization after application launch.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    searchRadius = [defaults floatForKey:@"SearchRadius"];
    returnResults = [defaults integerForKey:@"RecordReturnCount"];
    // ...
}


Without any other code, I can only suggest that you are not setting SearchRadius and RecordReturnCount correctly. They would be along the lines of:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat: searchRadius forKey: @"SearchRadius"];
[defaults setInteger: returnResults forKey: @"RecordReturnCount"];

It has been a while since I have used this but I don't recall it being that difficult. If you look in your Preferences you should see com.YOURCOMPANYHERE.YOURPROGRAM.plist - it is trivial to examine that to see where you may have gone wrong.

EDIT: BTW, I've never used a bundle to initialize settings - typically I'll have a flag named @"Initialized" - if you access it and it returns NO, then you haven't ever set up your user settings and can set them up... be sure to set @"Initialized" to YES!!! That would eliminate the need for a settings bundle.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜