循环遍历NSFetchRequest结果

时间:2012-10-16 09:42:00

标签: objective-c core-data

我对Core Data很新,实际上是Objective-C,所以我希望我正确地说这个,我已经能够使用Core Data存储和检索数据了,但是我正在努力循环结果集,

// read the data
NSFetchRequest *req = [[NSFetchRequest alloc]init];
[req setEntity:[NSEntityDescription entityForName:@"Card" inManagedObjectContext:app.managedObjectContext]];
//[req setPredicate:[NSPredicate predicateWithFormat:@"cardID == %@", @"1"]];
NSArray *crd = [[app.managedObjectContext executeFetchRequest:req error:nil] lastObject];

for(Card *card in crd){
    NSLog(@"%@",card.backImageURL);
}

当我尝试运行应用程序时,我一直收到此错误,

2012-10-16 11:37:29.407 Core test[925:17903] -[Card countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x962f5c0
2012-10-16 11:37:29.408 Core test[925:17903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Card countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x962f5c0'

我确信这是非常简单的事情但由于某种原因我似乎无法确定问题。

1 个答案:

答案 0 :(得分:1)

而不是

NSArray *crd = [[app.managedObjectContext executeFetchRequest:req error:nil] lastObject];

应该是

NSArray *crd = [app.managedObjectContext executeFetchRequest:req error:nil];

如果收到错误,则应使用该错误参数 -

NSError *error = nil;
NSArray *crd = [app.managedObjectContext executeFetchRequest:req error:&error];
if(error){
    NSLog(@"ERROR: %@", [error localizedDescription]);
}