Cocoa过滤器NSPredicate的完全用法
Cocoa用NSPredicate描述查询的方式,原理类似于在数据库中进行查询
计算谓词:
//基本的查询
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
//在整个cars里面循环比较
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *cars = [garage cars];
for (Car *car in [garage cars]) {
if ([predicate evaluateWithObject: car]) {//谓词查询语句
NSLog (@"%@", car.name);
}
}
//输出完整的信息
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *results;
results = [cars filteredArrayUsingPredicate: predicate];//谓词搜索语句
NSLog (&#
iOS 谓词查询NSPredicate
最新推荐文章于 2020-12-08 10:45:39 发布