-[NSUserDefault registerDefaults:] 不起作用?
我的代码如下:
- (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;
}
再往下,我用如下代码轮询 NSUserDefaults:
NSUserDefaults * userDefaults = [[NSUserDefaults standardUserDefaults] retain];
NSNumber* optionsBoolValue = [userDefaults objectForKey:@"show_outer_reference_circle_preference"];
NSAssert(optionsBoolValue != nil, @"AAAAHH");
//...
[userDefaults release];
并且设置不通过。正如您所看到的,我将值设置为 YES,但是该设置显示为 nil,并且应用程序的运行类似于“AAAAHH”。
我做错了什么?
预先感谢,
-尼克
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所看到的,代码的第一部分紧接在 didFinishLaunching 之后。但是,代码的“读出”部分(问题中的第二个块)位于由于加载笔尖而创建的对象的 init 方法中,因此在 before didFinishLaunching,因此会像“AAAAHHH”。
为了纠正这个问题,我创建了一个对象,其中包含一个用于设置 userDefaults 的静态方法,并在读取任何设置之前调用该方法。
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.