2012-04-27 70 views
0

我的數據庫中有一個名爲「Me」的實體,它有一個名爲「loggedIn」的BOOL屬性。我該屬性的默認值設置爲NO,像這樣:爲什麼我的實體的BOOL類型屬性返回YES,即使它被設置爲NO?

enter image description here

只是爲了確保它被設置爲NO,我還要把它設置程序,但它仍然是顯示了YES日誌中:

NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Me"]; 

NSError *err; 
NSUInteger count = [context countForFetchRequest:request error:&err]; 

if (count == 0) { 
    NSManagedObject *me = [NSEntityDescription insertNewObjectForEntityForName:@"Me" inManagedObjectContext:context]; 
} 
NSArray *fetchedObjects = [context executeFetchRequest:request error:&err]; 

[[fetchedObjects objectAtIndex:0] setValue:[NSNumber numberWithBool:NO] forKey:@"loggedIn"]; 
NSString *amIYesOrNo = [NSString stringWithFormat:@"%@", ([[fetchedObjects objectAtIndex:0] valueForKey:@"loggedIn"] ? @"YES" : @"NO")]; 

NSLog(amIYesOrNo); 

amIYesOrNo always should show up YES when it should be no。到底是怎麼回事?

回答

4

valueForKey:@「loggedIn」正在向您返回一個有效的對象,然後您的?:操作符正在檢查它是否爲非零,並且它始終是。您需要對NSNumber應用boolValue方法調用以獲取它所包含的內容。

+0

你能舉個例子嗎? – zakdances 2012-04-27 15:39:30

+0

'BOOL yesOrNo = [[NSNumber numberWithBool:NO] boolValue];' – 2012-04-27 15:44:03

相關問題