简体   繁体   English

`[NSUserDefaults standardUserDefaults]`返回nil

[英]`[NSUserDefaults standardUserDefaults]` returns nil

I want to save some user preferences, but 我想保存一些用户首选项,但是

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

just returns nil . 只是返回nil

iOS-Developper Library says, that this should return the existing shared defaults object or create one if none exists... What am I missing? iOS开发人员库说,这应该返回现有的共享默认对象,或者如果不存在则创建一个对象...我想念什么?

I also use Appirater and there all this stuff seems also not to work... 我也使用Appirater ,所有这些东西似乎也不起作用...

This code gets called when the user pushes a button... 用户按下按钮时将调用此代码...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int index = ([defaults integerForKey:@"BackgroundColorSpecifier"]+ 1)%self.backgroundColors.count;
[defaults setInteger:index forKey:@"BackgroundColorSpecifier"];
[defaults synchronize];

This gets called in application: didFinishLaunchingWithOptions: 这在application: didFinishLaunchingWithOptions:被调用application: didFinishLaunchingWithOptions:

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults registerDefaults:@{@"BackgroundColorSpecifier": @0}];
[standardDefaults synchronize];

When I debug this code snippets the green "position-indicator" jumps around in a very strange manner... 当我调试此代码片段时,绿色的“位置指示器”以一种非常奇怪的方式跳来跳去...

I don't have any clue, whats going on... Please help! 我没有任何线索,怎么回事...请帮助!

This is far more likely to be a problem with the debugger than anything else, particularly with your other issues. 调试器比其他任何问题都更可能是问题,尤其是其他问题。 I've seen similar things in my own projects but don't have a reliable way of clearing it out other than the usual restart / clean options. 我在自己的项目中看到过类似的情况,但是除了通常的重新启动/清除选项之外,没有清除它的可靠方法。

NSLogs will usually give more consistent results than the debugger if the debugger is having an off day. 如果调试器休息日,NSLogs通常会提供比调试器更一致的结果。

NSUserDefaults isn't broken. NSUserDefaults没有损坏。 We'd have heard about it by now. 到目前为止,我们已经听说过。

you can use this function to log you userDefaults dic 您可以使用此功能记录您的用户默认值dic

 - (void)logCache
    {
        NSDictionary * dic = [[NSBundle mainBundle] infoDictionary];
        NSString *bundleId = [dic  objectForKey: @"CFBundleIdentifier"];
        NSUserDefaults *appUserDefaults = [[NSUserDefaults alloc] init];
        NSDictionary *cacheDic = [appUserDefaults persistentDomainForName: bundleId];
        NsLog(@"cacheDic::%@",cacheDic);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM