我有这样的请求:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY attributes.attribute.attributeId IN %@", attributeIds];
这将返回一个对象列表,其中包含我设置的一个或多个属性。我想得到一个包含我传递的所有属性的对象列表,所以我尝试了:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL attributes.attribute.attributeId IN %@", attributeIds];
但我有一个例外:
'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'
无论如何,我甚至不确定这是正确的要求。假设我有属性列表:[red, green, blue]
,如何获得至少具有这些属性的所有对象?
* Object_1 (red, green, blue)
* Object_2 (red, green, blue, yellow, brown)
* Object_3 (red, green blue, black, brown)
但不是Object_4 (red, green, yellow)
,因为它没有blue
属性(请注意,我按照预期获得了ANY
获取请求的所有4个对象。
编辑,相关问题:如果我想完全匹配怎么办?因此,对于 [red, green, blue]
,我只会获得Object_1
?
编辑2:我设法回答了这两个问题,但I have a new one
答案 0 :(得分:5)
NSPredicate *objectsThatContainsAtLeastAllAttributesInList =
[NSPredicate predicateWithFormat:
@"SUBQUERY(attributes, $s, $s.attribute.attributeId IN %@).@count == %d", attributeIds, [attributeIds count]];
NSPredicate *objectsWhoseAttributesAreInList =
[NSPredicate predicateWithFormat:
@"attributes.@count == %d AND SUBQUERY(attributes, $s, $s.attributes.id IN %@).@count == %d", [attributeIds count], attributeIds, [attributeIds count]];