James P. Wright
James P. Wright

Reputation: 9131

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.

Upvotes: 0

Views: 67

Answers (1)

BoltClock
BoltClock

Reputation: 723809

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

Upvotes: 2

Related Questions