0

我对 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'

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

4

1 回答 1

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]);
}
于 2012-10-16T09:55:27.553 回答