Oh *_*Boy 63 core-data nspredicate ios
我有两个名为"Category"和"Article"的实体,它们有很多关系.我想形成一个谓词,搜索category.name等于某个值的所有文章.我有以下内容:
NSEntityDescription *entityArticle = [NSEntityDescription entityForName:@"Article" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categories.name == [cd] %@", category.name];
[request setSortDescriptors:sortDescriptors];
[request setEntity:entityArticle];
[request setPredicate:predicate];
NSMutableArray *results = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
if ([results count] > 0)
NSLog(@"Results found.");
else
NSLog(@"NO results found.");
[request release];
[sortDescriptor release];
[sortDescriptors release];
Run Code Online (Sandbox Code Playgroud)
我收到的错误是 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
有没有选择来检索所需的数据?
Dav*_*ong 148
您正在尝试将collection(categories.name
)与标量值(category.name
)进行比较.您需要使用集合比较器(CONTAINS
),或使用谓词修饰符(ANY
/ ALL
/ SOME
等).
尝试使用:
[NSPredicate predicateWithFormat:@"ANY categories.name =[cd] %@", category.name];
Run Code Online (Sandbox Code Playgroud)
要么:
[NSPredicate predicateWithFormat:@"categories.name CONTAINS[cd] %@", category.name];
Run Code Online (Sandbox Code Playgroud)
小智 6
快速语法
万一有人像我一样迅速地看到这篇文章......
let predicate = NSPredicate(format: "ANY categories.name = %@", category.name!)
fetchRequest.predicate = predicate
Run Code Online (Sandbox Code Playgroud)
为我工作。
归档时间: |
|
查看次数: |
25821 次 |
最近记录: |