首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环遍历NSFetchRequest结果

循环遍历NSFetchRequest结果
EN

Stack Overflow用户
提问于 2012-10-16 17:42:00
回答 1查看 754关注 0票数 0

我对Core Data和Objective-C整体来说都是新手,所以我希望我的措辞是正确的,我已经能够使用Core Data存储和检索数据,但我正在努力遍历结果集。

代码语言:javascript
复制
// 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);
}

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

代码语言:javascript
复制
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'

我确信这是非常简单的事情,但由于某些原因,我似乎不能准确地指出问题所在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-16 17:55:28

而不是

代码语言:javascript
复制
NSArray *crd = [[app.managedObjectContext executeFetchRequest:req error:nil] lastObject];

它应该是

代码语言:javascript
复制
NSArray *crd = [app.managedObjectContext executeFetchRequest:req error:nil];

如果出现错误,则应使用error参数-

代码语言:javascript
复制
NSError *error = nil;
NSArray *crd = [app.managedObjectContext executeFetchRequest:req error:&error];
if(error){
    NSLog(@"ERROR: %@", [error localizedDescription]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12911757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档