当我们使用ALL / ANY等时,我正在尝试理解NSPredicate及其语法
我的班级人员:
@interface Person : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) NSString* surname;
@property NSUInteger age;
@property NSUInteger weight;
@property NSUInteger height;
我将它用于这样的谓词:
NSArray* persons = [NSArray arrayWithObjects:personOne, personTwo, personThree, personFour, nil];
我的谓词:
NSPredicate* predicateEight = [NSPredicate predicateWithFormat:@"ANY SELF.age > %@", @55];
filteredArray = [persons filteredArrayUsingPredicate:predicateEight];
NSLog(@"Age > 55 %@", filteredArray);
我收到了一个错误:
ALL或ANY运算符的左侧必须是a NSArray或NSSet
我需要如何更改代码? 在此先感谢您的帮助。
答案 0 :(得分:1)
它应该是这样的:
NSPredicate* predicateEight = [NSPredicate predicateWithFormat:@"self.age > %d", 55];
答案 1 :(得分:0)
)
ALL / ANY用于NSArray或NSSet。在你的情况下任何用于Person的例子。
使用示例ANY:
NSPredicate* predicateEight = [NSPredicate predicateWithFormat:@"ANY SELF.age > %@", @55];
BOOL result = [predicateEight evaluateWithObject:persons];
如果您有阵列人员(阵列阵列):
NSArray* group1 = [NSArray arrayWithObjects:personOne, personTwo, personThree, nil];
NSArray* group2 = [NSArray arrayWithObjects:personFour, personFive, personSix, nil];
NSArray* group3 = [NSArray arrayWithObjects:personSeven, personEight, personNine, nil];
NSArray* groups = [NSArray group1, group2, group3, nil];
NSPredicate* predicateEight = [NSPredicate predicateWithFormat:@"ANY SELF.age > %@", @55];
NSArray* filteredArray = [groups filteredArrayUsingPredicate:predicateEight];