2014-10-07 31 views
-1

喜的朋友我与核心数据的工作。当我运行以下消息应用程序崩溃,在iOS中的核心数据管理对象上下文

"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; 
} 
崩溃项目
+1

错误消息非常清楚,告诉你什么是错的。有时候检查方法的返回值是有意义的。 – 2014-10-07 06:15:16

+0

您的核心数据模型中没有名为“广告”的实体 – CW0007007 2014-10-07 07:15:48

回答

-1

我会改变viewDidLoad语句[super viewDidLoad];是第一个。其他一切对我来说都很好。希望这可以帮助

+0

“viewDidLoad”调用的顺序/位置与OP在此试图解决的问题无关。如果您的代码相关建议没有真正回答问题,请考虑针对问题写一条评论。 – ttarik 2014-10-07 07:19:16

相关问题