开发者

Cannot figure out what simple thing I am doing wrong with this CoreData object

I have a CoreData object called "User". This object has a property declared with @property (nonatomic, retain) NSNumber * isloggedin; in User.h and in User.m I have @dynamic isloggedin;

In my AppDelegate I have this method:

- (NSManagedObject *) getLoggedInUserFromCurrentServer {
NSManagedObject *theUser = nil;

    for (User *user in [[self myServer] users]) {
        if (user.isloggedin == [NSNumber numberWithBool:YES]) {
            // we found it
            theUser = user;
            break;
        }
    }

    return theUser;
}  

I have looked at the table in my database and there is only a single user and only a single server. That user has ZISLOGGEDIN set to 1.

When this code goes through, the IF statement says false.

[NSNumber numberWithBool:YES] returns 1 if I开发者_如何学编程 po it.

If I po user.isloggedin or po [user isloggedin] I get no member named isloggedin and Target does not respond to this message selector.

I had this code working before, but changed it and cannot figure out what I did before that made this work...or for that sake, why this won't work. I'm sure I'm missing some insanely obvious thing here...but I can't find it.


Simple: NSNumber is a pointer type and so by using == to compare, you're actually comparing pointers to distinct NSNumber objects. Thus, even if both contain the same BOOL values, your comparison won't evaluate to YES.

To compare their primitive BOOL types, use boolValue:

for (User *user in [[self myServer] users]) {
    if ([user.isloggedin boolValue] == YES) {
        // we found it
        theUser = user;
        break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜