我正在做一个核心数据教程,我一直在崩溃。这是一个objc_exception_throw。
我创建了一个名为loadTableData的方法并在viewDidLoad中调用它
-(void)loadTableData{
NSManagedObjectContext *context = [[self appDelegate]managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Label" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"label like %@", [context objectWithID: self.labelID]];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
self.artistArray = [context executeFetchRequest:fetchRequest error:&error];
[self.tableView reloadData];
}它会被困在这里
self.artistArray = [context executeFetchRequest:fetchRequest error:&error];注释掉谓词alloc/init和setPredicate方法调用会产生一个应用程序,它不会崩溃,但不会做我想做的事情。
请参阅下面的实体和关系。

在LabelViewController中,这里有一些额外的代码来展示如何设置上下文objectWithID: self.labelID
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MLBLArtistViewController *artistViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ArtistViewController"];
Label *label = [self.labelArray objectAtIndex:indexPath.row];
artistViewController.labelID = [label objectID];
[self.navigationController pushViewController:artistViewController animated:YES];
}发布于 2013-12-17 09:03:56
首先,您似乎想要获取“艺术家”对象,而不是“标签”对象:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:context];接下来,LIKE或CONTAINS用于测试字符串,您需要一个简单的==:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"label == %@", [context objectWithID:self.labelID]];备注:将label对象本身传递给推送视图控制器(而不是[label objectID] )会更简单。
发布于 2013-12-17 08:27:47
我会用CONTAINS代替:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"label CONTAINS[cd] %@", [context objectWithID: self.labelID]];您可以使用一个类似的,但我喜欢简单的包含,参见下面的问题:NSPredicate that is the equivalent of SQL's LIKE
https://stackoverflow.com/questions/20629150
复制相似问题