我試圖找到一個匹配字符串和一組對象的對象。我的謂詞如下所示:核心數據謂詞對多
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and individuals CONTAINS %@", name, individuals];
我沒有收到任何匹配。雖然我知道有一個匹配名稱和個人設置的實體。
我的謂詞有什麼問題?
編輯:我設法做了一些進步。現在的問題是,如果我試圖找到一個具有現有組名稱和現有聯繫人的組,即groupname =「test」和individuals.name =「john doe」,並且individuals.contactInfo =「123」,它會正確地找到該組但如果我有一個具有相同組名和相同聯繫人的組+另一個聯繫人,它也會找到我這個我不想要的組。
我只想要與謂詞完全匹配的組。
我現在使用subpredicates通過這樣做:
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
我以爲SUBQUERY可以用來過濾一個基於謂詞的集合。 Apple的例子:(SUBQUERY(居民,$ x,$ x.firstname ==「Jane」&& $ x.lastname ==「Doe」)。@ count!= 0)。我不認爲這將在給定的元素列表上工作。 – diederikh
這個謂詞對我不起作用,如果我有一個包含兩個人的組,這個謂詞將返回這個組,即使我只提供了這個組中的一個人。 –
我想我在另一個線程中找到了我的問題的確切答案,因此我將此答案標記爲已接受。 (在這裏回答:http://stackoverflow.com/a/13086353/294661) –