请帮忙!我一直试图解决这个问题太久了。
我似乎无法在fetchedResultsController方法中使用NSPredicate:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) return fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"column == 0"];
[fetchRequest setPredicate:predicate];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController; }
然而,在另一种方法中,我只是检查一个对象是否存在,谓词就像一个魅力:
- (BOOL)findObjectWithKey:(NSString *)key andValue:(NSString *)value sortBy:(NSString *)sort {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%K == %@", key, value];
[request setPredicate:predicate];
[request setReturnsObjectsAsFaults:NO];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sort ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
NSError *error = nil;
NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];
[aFetchedResultsController release];
[request release];
[sortDescriptor release];
[sortDescriptors release];
if ((result != nil) && ([result count]) && (error == nil)) {
return TRUE;
} else {
return FALSE;
}}
我做错了什么?提前谢谢!
答案 0 :(得分:0)
假设您的“column”属性是一个数字,您应该将它与NSNumber对象进行比较,如下所示:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"column == %@",[NSNumber numberWithInteger:0]];
答案 1 :(得分:0)
由于column是实体上的属性,因此您的谓词逻辑很好。你看到什么结果表明谓词不起作用?