ios-NSPredicate谓词过滤筛选

879 阅读1分钟

谓词(NSPredicate)是OC针对数据集合的一种逻辑帅选条件,类似一个过滤器

Person * p1 = [Person personWithName:@"alex" Age:20];
...
Person * p5 = [Person personWithName:@"alex4" Age:80];
    
NSArray * persons = @[p1, p2, p3, p4, p5];
//定义谓词对象,谓词对象中包含了过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < 30"];
//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];

www.jianshu.com/p/0e787ab6e…