首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSPredicate,executeFetchRequest崩溃

NSPredicate,executeFetchRequest崩溃
EN

Stack Overflow用户
提问于 2013-12-17 08:20:21
回答 2查看 1.4K关注 0票数 3

我正在做一个核心数据教程,我一直在崩溃。这是一个objc_exception_throw。

我创建了一个名为loadTableData的方法并在viewDidLoad中调用它

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

它会被困在这里

代码语言:javascript
复制
self.artistArray = [context executeFetchRequest:fetchRequest error:&error];

注释掉谓词alloc/init和setPredicate方法调用会产生一个应用程序,它不会崩溃,但不会做我想做的事情。

请参阅下面的实体和关系。

在LabelViewController中,这里有一些额外的代码来展示如何设置上下文objectWithID: self.labelID

代码语言:javascript
复制
-(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];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-17 09:03:56

首先,您似乎想要获取“艺术家”对象,而不是“标签”对象:

代码语言:javascript
复制
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:context];

接下来,LIKECONTAINS用于测试字符串,您需要一个简单的==

代码语言:javascript
复制
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"label == %@", [context objectWithID:self.labelID]];

备注:将label对象本身传递给推送视图控制器(而不是[label objectID] )会更简单。

票数 0
EN

Stack Overflow用户

发布于 2013-12-17 08:27:47

我会用CONTAINS代替:

代码语言:javascript
复制
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"label CONTAINS[cd] %@", [context objectWithID: self.labelID]];

您可以使用一个类似的,但我喜欢简单的包含,参见下面的问题:NSPredicate that is the equivalent of SQL's LIKE

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20629150

复制
相关文章

相似问题

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