BOOL NSUserDefaults的逻辑 [英] NSUserDefaults BOOL logic

查看:207
本文介绍了BOOL NSUserDefaults的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个竞猜游戏,当回答是正确的,剔带符号的画面就会出现。因为我有许多领域,需要应用程序记住哪些已经做了一次发射。我使用NSUserDefaults的。这是伟大的工作,但问题是,在这非常首次推出的所有完成的显示图像。我试图让周围的布尔逻辑,但没有成功。

I have a quiz game, where when the answer was correct, a picture with tick sign will appear. As I have many fields and need the app to remember which are done already for next launch. I am using NSUserDefaults. It is working great, but the problem is, that at the very first launch all "done" images are displayed. I was trying to get around the BOOL logic but no success.

.H

BOOL doneState;

.M

- (void)viewDidLoad{

    [super viewDidLoad];
    [self doneHidden];
    [self checkBools];
} 

- (void)checkBools{

    //--------NSUserDefaults & tick sign hidden
    BOOL saved = [[NSUserDefaults standardUserDefaults] boolForKey:@"hiddenDone"];
    NSLog (@"Value of my saved BOOL = %@", saved ? @"YES" : @"NO");
    done.hidden = saved;
}

-(void)doneHidden{
   done.hidden = YES;
}

当用户退出应用程序,方法如下能够记忆BOOL值

When user quits the app, method below memorises the BOOL value

-(IBAction)flipBack:(id)sender{

    if (done.hidden == NO) {
        doneState = NO;
    }
    else {
        doneState = YES;
    }

    [[NSUserDefaults standardUserDefaults] setBool:doneState forKey:@"hiddenDone"];
    NSLog (@"Value of BOOL 1  = %@", doneState ? @"YES" : @"NO");
}

我们的想法是第一隐藏名为完成的ImageView的,然后通过布尔运行。控制台提供了关于第一个推出的,它没有隐藏的结果,即使它被设置这种方式。

The idea is to first hide the imageView named "done" and then run through the BOOL. The console gives result on the very first launch that it is not hidden, even though It is set that way.

控制台输出:

Value of my saved BOOL = NO;

我怎样才能提高它的逻辑是什么?

How can I improve the logic of it?


  • 在第一个推出的隐藏图像

  • 如果退出应用程序检查,如果图像是可见的,并创建BOOL

  • 接下来推出阅读BOOL和显示图像如果previously保存,以便

推荐答案

有三个选项:


  1. 更改的 BOOL showImage 的意思,所以你把它设置为 TRUE 当你要显示的图像。

  2. $ P $在 P-注册此密钥的 TRUE 价值LI>
  3. 而不是使用 boolForKey:,使用 objectForKey:和第一次检查,如果对象是(=尚未确定),并且仅当非空,使用 - [NSNumber的boolValue] 来获得布尔值

  1. Change the meaning of the BOOL to showImage so you set it to TRUE when you want to show the image.
  2. Pre-register a TRUE value for this key in -[NSUserDefaults registerDefaults:]
  3. Instead of using boolForKey:, use objectForKey: and first check if the object is nil (= not yet set), and only if non-nil, use -[NSNumber boolValue] to get the boolean value.

这篇关于BOOL NSUserDefaults的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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