从Parse中的用户类中检索布尔值

时间:2014-08-07 19:01:10

标签: ios parse-platform

我无法弄清楚如何从Parse中的用户类中检索布尔值。这段代码对我不起作用:

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

任何人都知道这样做的正确方法吗?

2 个答案:

答案 0 :(得分:2)

你所做的一切看起来都很好。设置变量/保存用户的方式可能是错误的。尝试这样的事情,看看它是否有效。

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);
    }
}];

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

[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);
    }
}];

答案 1 :(得分:0)

试试这个:

PFUser *user = [PFUser currentUser];
BOOL boolean = [[user objectForKey@"bool"] boolValue];