Sti*_*and 16 core-data objective-c nspredicate ios
基于下面的数据模型

并且基于用户输入,我创建了一个名为selectedTags的实体Tag的managedObjects的NSSet .
[NSPredicate predicateWithFormat:@"ANY entryTags IN %@", selectedTags];
Run Code Online (Sandbox Code Playgroud)
...这将返回任何条目,其中至少有一个entryTag集合中的entryTag.
我想要的东西是:
[NSPredicate predicateWithFormat:@"ALL entryTags IN %@", selectedTags];
Run Code Online (Sandbox Code Playgroud)
...注意唯一的变化是"任何"到"全部".这说明了我想要的,但不起作用.
为了制定我期望的结果:
我正在寻找一种解决方案,它只会返回条目标签,这些条目的条目都在selectedTags列表中(但同时,如果可能的话,不一定是相反的方式).
进一步说明:
(标签)妈妈
(标签)爸爸
(标签)礼物
(入门)她是她.....(标签)妈妈
(入职)他是他........(标签)爸爸
(入职)给妈妈的礼物......(标签:)妈妈,送给
父亲的礼物(入职).....(标签:)爸爸,礼物
如果selectedTags包含"妈妈"和"礼物",那么"爸爸的礼物"条目将会显示,因为它有"礼物"标签.我宁愿让它不显示:)
Sti*_*and 28
到目前为止,这是明确的答案:
[NSPredicate predicateWithFormat:@"SUBQUERY(entryTags, $tag, $tag IN %@).@count = %d", selectedTags, [selectedTags count]];
Run Code Online (Sandbox Code Playgroud)
美丽.
感谢Dave DeLong.
如何使用复合谓词?据我所知,您希望返回所有与标签列表匹配的条目,而不仅仅是其中任何一个.一种方法是为每个标签创建一个谓词,然后使用复合谓词将它们组合在一起.
NSMutableArray *predicates = [[NSMutableArray alloc] init];
for (Tag *tag in selectedTags) {
[predicates addObject:[NSPredicate predicateWithFormat:@"ANY entryTags.tagName MATCHES %@", tag.tagName]];
}
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
Run Code Online (Sandbox Code Playgroud)
这应该实现你想要的.然后根据您的请求设置此谓词.
| 归档时间: |
|
| 查看次数: |
8379 次 |
| 最近记录: |