NSPredicate字符串中的isSubsetOfSet [英] isSubsetOfSet in NSPredicate string
问题描述
我很难检查nsorderedset是否是NSPredicate字符串中另一个nsorderedset的子集.
I have difficulties checking if a nsorderedset is a subset of another nsorderedset in a NSPredicate string.
我可以通过以下代码块实现结果:
I can achieve the result with the block below:
request.predicate = [NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary *bindings) {
NSOrderedSet *biggerSet = [object valueForKey:@"attributeName"];
return [smallerSet isSubsetOfOrderedSet:biggerSet];
}];
但是我相信字符串格式的性能可能更高(我错了吗?).
but I believe that in string format could be more performant (am I wrong?).
尝试了诸如@属性CONTAINS%@"和@%@ IN属性"之类的东西,但是没有运气.
tried things like @"attribute CONTAINS %@" and @"%@ IN attribute" but with no luck.
感谢您的帮助.
推荐答案
类似的方法应该起作用:
Something like this should work:
NS(Ordered)Set *smallerSet = …
request.predicate =
[NSPredicate predicateWithFormat:@"SUBQUERY(attribute, $a, $a IN %@).@count == %lu",
smallerSet, (unsigned long)[smallerSet count]];
谓词检查 smallerSet
中包含的属性"值的数量是否为等于 smallerSet
中的元素数.如果是,则 smallerSet
必须是属性"集的子集.
The predicate checks if the number of "attribute" values contained in smallerSet
is
equal to the number of elements in smallerSet
. If yes, then smallerSet
must be
a subset of the "attribute" set.
基于字符串的谓词通常不会比基于块的谓词快(比较 https://stackoverflow.com/a/21158730/1187415 ).但是核心数据获取请求(带有SQlite存储文件)不能使用基于块的谓词.
A string based predicate is not generally faster than a block based (compare https://stackoverflow.com/a/21158730/1187415). But a Core Data fetch request (with a SQlite store file) cannot use block based predicates.
这篇关于NSPredicate字符串中的isSubsetOfSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!