-[NSUserDefault registerDefaults:] 不起作用?

发布于 2024-09-27 03:27:22 字数 1084 浏览 0 评论 0 原文

我的代码如下:

- (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”。

我做错了什么?

预先感谢,

-尼克

My code is as follows:

- (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;
}

and further down I poll NSUserDefaults with code as follows:

    NSUserDefaults * userDefaults = [[NSUserDefaults standardUserDefaults] retain]; 
    NSNumber* optionsBoolValue = [userDefaults objectForKey:@"show_outer_reference_circle_preference"];
    NSAssert(optionsBoolValue != nil, @"AAAAHH");
    //...
    [userDefaults release];

And the settings don't come through. As you can see, I set the value to YES, buuuuttt the setting comes through as nil and the app goes like "AAAAHH".

What am I doing wrong?

Thanks in advance,

-Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2024-10-04 03:27:22

正如您所看到的,代码的第一部分紧接在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文