iPhone开发人员-NSUserDefaults检查布尔值是否存在 [英] iphone dev - NSUserDefaults check for boolean existence

查看:74
本文介绍了iPhone开发人员-NSUserDefaults检查布尔值是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将设置捆绑包添加到我的应用中,但无法读取bool设置.我知道在启动该应用程序时,除非用户实际输入设置,否则不会读取设置-这就是我要捕获的内容.

I've just added a settings bundle to my app and am having trouble reading the bool settings. I know that upon launch of the app, the settings are not read unless the user actually enters them - and that's what I am trying to capture.

但是,如果答案是否定的或尚未设置,我的代码只是在捕获.我需要找出是否已设置好,然后设置答案!

However, my code is simply capturing if the answer is NO OR they havent been set. I need to find out if they've been set, THEN set answers!

设置代码:

BOOL playSound;
BOOL playVibrate;


//test for some defaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:@"pref_sound"]) {
    playSound = YES;
    playVibrate = YES; 
} else {
    playSound = [prefs boolForKey:@"pref_sound"];
    playVibrate = [prefs boolForKey:@"pref_vibrate"]; 
}


if (playSound) {
//do stuff
}

问题是,如果用户将设置设置为否",则代码会将振动和声音都更改为是"-这意味着要捕获不"设置....

the problem is, if the user sets the settings to "NO", the code then changes both vibrate AND sound to yes - which is meant to be the capture for NOT setting....

有什么想法吗?

推荐答案

首先,您的if条件中存在错误.您正在根据

First of all you have a bug in your if conditional. You're checking the boolean value itself which, according to the boolForKey: documentation, will return a NO if it's not set yet. So boolForKey: is not the right way to do that.

还有另外两个想法.

请考虑将另一个设置与另一个键一起使用,以指定您的设置是否已初始化.在启动应用程序或首次阅读设置时进行检查.如果需要初始化.例如:

Consider using another setting with another key to specify whether your settings have been initialized. Check it when you launch your app, or when you first read a setting. Initialize if needed. For instance:

- (void) initializeUserDefaults {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (nil == [defaults objectForKey:@"initialized_defaults"]) {
        [defaults setBool:YES forKey:@"pref_sound"];
        [defaults setBool:YES forKey:@"pref_vibrate"];
        [defaults setObject:@"dummy_value" forKey:@"initialized_defaults"];
    }
}

一个更简单的解决方案(但我不确定这是否可行)将更改您的条件为:

A simpler solution (but I'm not sure if this would work) would be to change your conditional to read:

if (![prefs objectForKey:@"pref_sound"]) {

同样,我不知道这是否会实现我想像的效果,但是我想像objectForKey:将返回底层的装箱布尔对象(如果存在的话),或者返回nil.

Again I don't know if this will do what I imagine it will, but I imagine that objectForKey: will return the underlying boxed boolean object, if it's there, or nil.

如果您在新版本的应用程序中添加新设置,则不希望未初始化新设置,也不想浪费用户的现有设置.第二种方法使这一工作变得轻松,第一种方法更容易搞砸.但是第一种方法也将所有设置都收集到一个位置,这样您就可以看到它的工作方式,这是我喜欢的.

If you add a new setting in a new version of your app, you don't want to leave your new settings uninitialized and you don't want to stomp your users' existing settings. This second method makes that effortless, and in the first method it's easier to screw up. But the first method also gathers all your settings in one place so you can see how it's supposed to work, which I like.

我不确定哪些ADC文档会考虑最佳实践.为了找出答案,我建议您看看从NSUserDefaults类参考引用的任何代码示例.

I am not sure what ADC docs would consider a best practice though. To find that out, I'd suggest you look at any code samples referenced from the NSUserDefaults class reference.

这篇关于iPhone开发人员-NSUserDefaults检查布尔值是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆