从Parse中的用户类中检索布尔值 [英] Retrieve boolean from user class in Parse

查看:66
本文介绍了从Parse中的用户类中检索布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何从Parse中的用户类中检索布尔值.该代码对我不起作用:

I can't figure out how to retrieve a boolean from the user class in Parse. This code doesn't work for me:

PFUser *user = [PFUser currentUser];
NSNumber *boolNumber = [user objectForKey:@"bool"];
BOOL b = [boolNumber boolValue];
NSLog(@"%d", b);

有人知道这样做的正确方法吗?

Anyone know the proper way to do this?

推荐答案

您所做的一切在检索时看起来都不错.您设置变量/保存用户的方式可能有误.尝试这样的事情,看看是否可行.

Everything you're doing looks fine on retrieval. It's probably an error with the way you are setting the variable/saving the user. Try something like this and see if it works.

PFUser *user = [PFUser currentUser];
user[@"likesFruit"] = @(YES); // You can set it to @(NO) also. It doesn't matter. This is just an example.
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        BOOL likesFruit = [user[@"likesFruit"] boolValue];
        NSLog(@"Does this user like fruit?\n%@", (likesFruit) ? @"Yes" : @"No");
    } else {
        NSLog(@"Error saving user: %@", error);
    }
}];

确保检索安全的另一种方法是:

Another way to make sure your retrieval is safe is this:

[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error) {
        BOOL likesFruit = [object[@"likesFruit"] boolValue];
        NSLog(@"This user %@ like fruit", likesFruit ? @"does" : @"doesn't");
    } else {
        NSLog(@"Error retrieving user data: %@", error);
    }
}];

这篇关于从Parse中的用户类中检索布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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