朋友们,我正在处理核心数据。当我运行项目时,它会崩溃并显示以下消息
"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;
}
答案 0 :(得分:-1)
我会在viewDidLoad中将语句[super viewDidLoad];
更改为第一个。其他一切看起来都不错。希望这有帮助