我正在尝试找到匹配字符串和一组对象的对象。我的谓词看起来像这样:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and individuals CONTAINS %@", name, individuals];
我没有任何点击。虽然我知道有一个匹配名称和个人设置的实体。
我的谓词有什么问题?
编辑:我已经设法取得一些进展。现在的问题是,如果我尝试找到一个具有现有组名和现有联系人的组,即groupname =“test”和personal.name =“john doe”和personal.contactInfo =“123”,它将正确地找到该组但是,如果我有一个具有相同组名,相同联系人+另一个联系人的组,它也会找到我不想要的该组。
我只想要与谓词完全匹配的组。
我现在通过这样做来使用子谓词:
NSMutableArray *subPredicates = [NSMutableArray initWithCapacity:5];
// Add group name to predicate
[subPredicates addObject:[NSPredicate predicateWithFormat:@"name == %@", name]];
for (NSDictionary *contactInfo in individuals) {
NSString *name = [contactDict objectForKey:@"name"];
NSString *contactInfo = [contactDict objectForKey:@"contactInfo"];
NSPredicate *individualPredicate = [NSPredicate predicateWithFormat:@"ANY individuals.name LIKE %@ AND any individuals.contactInfo LIKE %@", name, contactInfo];
[subPredicates addObject:individualPredicate];
}
NSPredicate *individualsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
Group *group = // do the fetch with the predicate
最佳答案
我在电话上输入此内容,无法验证,但以下谓词可能对您有用:
[NSPredicate predicateWithFormat:@"name == %@ AND SUBQUERY(individuals, $x, $x IN %@).@count == %d",
name, individuals, individuals.count];
它搜索具有给定名称的对象,这些对象的个体是给定集合的超集。
关于objective-c - 核心数据谓词对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13124738/