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;
- (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.
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;
}
}
精彩评论