iOS谓词NSPredicate的简单使用

 1 #import "ViewController.h"
 2 #import "Person.h"
 3 
 4 @interface ViewController ()
 5 
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     //NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式。它的查询语言就像SQL的WHERE和正则表达式的交叉一样,提供了具有表现力的,自然语言界面来定义一个集合被搜寻的逻辑条件
13     NSArray * firstNames = @[@"Alice", @"Bob", @"Charlie", @"Quentin"];
14     NSArray * lastNames = @[@"Smith", @"Jones", @"Smith", @"Alberts"];
15     NSArray * ages = @[@24, @27, @33, @31];
16     NSMutableArray * people = [NSMutableArray array];
17     [firstNames enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
18         Person * person = [[Person alloc] init];
19         person.firstName = firstNames[idx];
20         person.lastName = lastNames[idx];
21         person.age = ages[idx];
22         [people addObject:person];
23     }];
24     NSPredicate * bobPredicate = [NSPredicate predicateWithFormat:@"firstName='Bob'"];
25     NSPredicate * smithPredicate = [NSPredicate predicateWithFormat:@"lastName=%@",@"Smith"];
26     NSPredicate * thirtiesPredicate = [NSPredicate predicateWithFormat:@"age>=30"];
27     
28     //["Bob Jones"]
29     NSLog(@"Bobs: %@",[people filteredArrayUsingPredicate:bobPredicate]);
30     //["Alice Smith", "Charlie Smith"]
31     NSLog(@"Smiths: %@",[people filteredArrayUsingPredicate:smithPredicate]);
32     //["Charlie Smith", "Quentin Alberts"]
33     NSLog(@"30's: %@",[people filteredArrayUsingPredicate:thirtiesPredicate]);
34     
35     /*
36      Foundation提供使用谓词(predicate)来过滤NSArray/NSMutableArray&NSSet/NSMutableSet的方法。
37      不可变集合,NSArray&NSSet,有可以通过评估接收到的predicate来返回一个不可变集合的方法filteredArrayUsingPredicate:和filteredSetUsingPredicate:
38      NSArray * array = [NSArray array];
39      [array filteredArrayUsingPredicate:bobPredicate];
40      NSSet * set = [NSSet set];
41      [set filteredSetUsingPredicate:bobPredicate];
42      
43      可变集合,NSMutableArray&NSMutableSet,可以使用方法filterUsingPredicate:它可以通过运行传递到的谓词来移除评估结果为FALSE的对象
44      NSMutableArray * tempArray = [NSMutableArray array];
45      [tempArray filterUsingPredicate:bobPredicate];
46      NSMutableSet * tempSet = [NSMutableSet set];
47      [tempSet filterUsingPredicate:bobPredicate];
48      NSDictionary可以用谓词来过滤它的键和值(两者都为NSArray对象)。NSOrderedSet可以由过滤的NSArray或者NSSet生成一个新的有序的集,或者NSMutableSet可以简单的removeObjectsInArray:,来传递通过否定predicate过滤的对象
49      
50      Core Data中使用NSPredicate
51      NSFetchRequest有一个predicate属性,它可以指定管理对象应该被获取的逻辑条件。谓词的使用规则在这里同样适用,唯一的区别在于,在管理对象环境中,谓词由持久化存储助理(persistent store coordinator)评估,而不像集合那样在内存中被过滤。
52      */
53     
54     //谓词语法
55     //替换
56     //%@是对值为字符串,数字或者日期的对象的替换值;%K是key path的替换值
57     
58     NSPredicate * ageIs33Predicate = [NSPredicate predicateWithFormat:@"%K=%@",@"age",@33];
59     // ["Charlie Smith"]
60     NSLog(@"Age 33: %@",[people filteredArrayUsingPredicate:ageIs33Predicate]);
61     //$VARIABLE_NAME是可以被NSPredicate -predicateWithSubstitutionVariables:替换的值。
62     NSPredicate *namesBeginningWithLetterPredicate = [NSPredicate predicateWithFormat:@"(firstName BEGINSWITH[cd] $letter) OR (lastName BEGINSWITH[cd] $letter)"];
63     // ["Alice Smith", "Quentin Alberts"]
64     NSLog(@"'A' Names: %@", [people filteredArrayUsingPredicate:[namesBeginningWithLetterPredicate predicateWithSubstitutionVariables:@{@"letter": @"A"}]]);
65     
66     /*
67      基本比较
68      =,== 左边的表达式和右边的表达式相等
69      >=,=>  左边的表达式大于或者等于右边的表达式
70      <=,=<  左边的表达式小于等于右边的表达式
71      >  左边的表达式大于右边的表达式
72      <  左边的表达式小于右边的表达式
73      !=,<> 左边的表达式不等于右边的表达式
74      
75      BETWEEN 左边的表达式等于右边的表达式的值或者介于它们之间。右边是一个有两个指定上限和下限的数值的数列(指定顺序的数列)。比如 1 BETWEEN{0,33},或者¥INPUT BETWEEN {$LOWER,$UPPER}。
76      
77      基本复合谓词
78      AND,&& 逻辑与
79      OR,||  逻辑或
80      NOT,!  逻辑非
81      
82      字符串比较
83      字符串比较在默认的情况下是区分大小写个音调的。你可以在方括号中用关键字符c和d来修改操作符以相应的指定不区分大小写和音调,
84      BEGINSWITH:左边的表达式以右边的表达式作为开始
85      CONTAINS:左边的表达式包含右边的表达式
86      ENDSWITH:左边的表达式以右边的表达式作为结束
87      LIKE:左边的表达式等于右边的表达式:?和*可作为通配符,其中?匹配一个字符,*匹配0个或者多个字符
88      MATCHES:左边的表达式根据ICU v3 的regex风格比较,等于右边的表达式
89      
90      合计操作
91      1关系操作
92      ANY,SOME:指定下列表达式中的任意元素。比如,ANY children.age < 18.
93      
94      */
95 }

自定义的Person类

1 #import <Foundation/Foundation.h>
2 
3 @interface Person : NSObject
4 
5 @property (nonatomic,copy)NSString * firstName;
6 @property (nonatomic,copy)NSString * lastName;
7 @property (nonatomic,strong)NSNumber * age;
8 
9 @end
1 #import "Person.h"
2 
3 @implementation Person
4 
5 - (NSString *)description{
6     return [NSString stringWithFormat:@"%@ %@",self.firstName,self.lastName];
7 }

猜你喜欢

转载自www.cnblogs.com/wanli-leon/p/12149096.html