我正在开发一个 Mac 应用程序,并且正在努力让谓词过滤器正常工作。对于 NSArrayController,使用 fetch 与过滤谓词的方式有什么不同吗?
我正在使用 Core Data 来存储我的记录,它们在我的表格 View 中完美显示。当我使用 fetch 谓词时,它会正确显示记录。当我使用过滤谓词时,它似乎没有做任何事情。需要明确的是 - 我一次使用一个谓词,但想在我的代码中显示这两个谓词。
entryController = [[NSArrayController alloc] init];
[entryController setManagedObjectContext:[self managedObjectContext]];
[entryController setAutomaticallyPreparesContent:YES];
[entryController setAutomaticallyRearrangesObjects:YES];
[entryController setClearsFilterPredicateOnInsertion:NO];
[entryController setEntityName:@"Entry"];
// only displays entries where deleted is not 1
[entryController setFetchPredicate:[NSPredicate predicateWithFormat:@"deleted != %@", [NSNumber numberWithInt:1]]];
// shows all entries regardless of the deleted status
[entryController setFilterPredicate:[NSPredicate predicateWithFormat:@"deleted != %@", [NSNumber numberWithInt:1]]];
if ([entryController fetchWithRequest:nil merge:YES error:&error] == NO) {
DLog(@"Error fetching entries: %@", error.description);
} else {
DLog(@"Found %lu entries.", [[entryController arrangedObjects] count]);
}
最佳答案
看来关键字“deleted”对于Core Data来说有着某种未知的意义。我重命名了我的属性(property)并解决了问题。这篇博文也提到了同样的问题:http://roobasoft.com/blog/2006/04/25/secret-core-data-reserved-keywords/
关于cocoa - NSArrayController 获取与过滤谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342048/