我的数据库中有一个名为“Me”的实体,它有一个名为“loggedIn”的 BOOL 属性。我将属性的默认值设置为 NO,如下所示:
只是为了确保它设置为 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);
当它应该是 NO 时,amIYesOrNo 总是显示 YES。到底是怎么回事?