在NSArray上评估NSPredicate(不过滤) [英] Evaluating an NSPredicate on a NSArray (without filtering)
本文介绍了在NSArray上评估NSPredicate(不过滤)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在NSArray上评估NSPredicate,而不必让NSPredicate开始过滤掉数组中的对象?
Is it possible to evaluate a NSPredicate on a NSArray, without having the NSPredicate starting to filter out objects in the array?
例如,说我有以下谓词,它们仅检查数组中对象的数量:
For instance, say I have the following predicate that just checks the number of objects in an array:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"count == 3"];
NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil];
BOOL match = [pred evaluateWithObject:list];
这将崩溃,因为pred会尝试从数组中的第一个对象而不是数组本身中检索计数"键.
This will crash, as pred will try to retrieve the "count" key from the first object in the array instead of the array itself.
推荐答案
使用 NSPredicate 的 SIZE 运算符,它等同于 count 方法 NSArray .
Use the SIZE operator of NSPredicate which is equivalent to count method of NSArray.
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF[SIZE] == 3"];
NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil];
BOOL match = [pred evaluateWithObject:list];
这篇关于在NSArray上评估NSPredicate(不过滤)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文