【问题标题】:app crashes in managed object context in core data in iOS应用程序在 iOS 核心数据的托管对象上下文中崩溃
【发布时间】:2014-12-01 11:09:56
【问题描述】:

朋友们,我正在处理核心数据。当我运行项目时,它会崩溃并显示以下消息

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name'Ad' "

我使用的以下代码

- (void)viewDidLoad
{
ads = [NSArray new];
[self fetchAllAds];
[super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [ads count];
}
-(NSArray *)fetchAllAds {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Ad" 
inManagedObjectContext:self.managedObjectContext]];
NSError *error = nil;
ads = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
return ads;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath
{ 
static NSString *CellIdentifier = @"ShoppingCart";
ShoppingCart *cell = (ShoppingCart*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath];
Ad *list = [ads objectAtIndex:indexPath.row];
NSLog(@"The string is %@",list.data);
cell.addesc.text=list.data;
cell.adId.text = [NSString stringWithFormat:@"%@",list.adId];
cell.adstatus.text = [NSString stringWithFormat:@"%@",list.state];
return cell;
}

【问题讨论】:

  • 错误信息非常清楚地告诉你出了什么问题。有时检查方法的返回值是有意义的。
  • 您的核心数据模型中没有名为“广告”的实体

标签: ios uitableview core-data ios7


【解决方案1】:

我会在 viewDidLoad 中将声明 [super viewDidLoad]; 更改为第一个。在我看来,其他一切都很好。希望这会有所帮助

【讨论】:

  • viewDidLoad 调用的顺序/位置与 OP 在此处尝试解决的问题无关。如果您有与代码相关的建议并没有真正回答问题,请考虑在问题上写评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多