谓词(搜索)

复制代码
 1 #import <Foundation/Foundation.h>
 2 #import "Person.h"
 3 
 4 int main(int argc, const char * argv[]) {
 5     @autoreleasepool {
 6         Person *person1 = [[Person alloc] initWithName:@"Mary" andAge:13 andGender:@"girl"];
 7         Person *person2 = [[Person alloc] initWithName:@"Tom" andAge:16 andGender:@"boy"];
 8         Person *person3 = [[Person alloc] initWithName:@"Linda" andAge:20 andGender:@"girl"];
 9         Person *person4 = [[Person alloc] initWithName:@"Jerry" andAge:32 andGender:@"boy"];
10         Person *person5 = [[Person alloc] initWithName:@"Sam" andAge:24 andGender:@"boy"];
11         
12         NSArray *personArr = @[person1, person2, person3, person4, person5];
13 #pragma mark 关系运算符
14         // 创建谓词,用于指定过滤条件 -- 年龄大于20
15         NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"age > 20"];
16         // 遍历数组
17         for (Person *person in personArr) {
18             // 判断person是否符合谓词里所定义的条件
19             if ([predicate1 evaluateWithObject:person]) {
20                 NSLog(@"person = %@", person);
21             }
22         }
23 #pragma mark 快速筛选数组
24         // 过滤数组,找到符合条件的元素,存到数组中
25         NSArray *resultArr = [personArr filteredArrayUsingPredicate:predicate1];
26         NSLog(@" > %@", resultArr);
27         
28 #pragma mark 逻辑运算符 and与 or或 not非
29         // 年龄大于20岁的女生
30         NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"age < 20 && gender = 'girl'"];
31         resultArr = [personArr filteredArrayUsingPredicate:predicate2];
32         NSLog(@"and%@", resultArr);
33         
34 #pragma mark 范围运算符 between在...之间  in包含的关系
35         // 年龄在16到24之间的人,包含16和24
36         NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"age between {16, 24}"];
37         resultArr = [personArr filteredArrayUsingPredicate:predicate3];
38         NSLog(@"between%@", resultArr);
39         
40         // 姓名是mary 和 tom的人
41         NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"name in {'Mary', 'Tom'}"];
42         resultArr = [personArr filteredArrayUsingPredicate:predicate4];
43         NSLog(@"in %@", resultArr);
44         
45 #pragma mark 占位符 $
46 //        NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"name = $Name"];
47         NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"age < $Age and gender = $Gender"];
48         NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Linda", @"Name", @20, @"Age", @"girl", @"Gender" ,nil];
49         predicate5 = [predicate5 predicateWithSubstitutionVariables:dictionary];
50         resultArr = [personArr filteredArrayUsingPredicate:predicate5];
51         NSLog(@"占位符%@", resultArr);
52         
53 #pragma mark 字符串运算符
54         NSArray *strArray = @[@"Hello", @"world", @"hey", @"kitty"];
55         // 以字符串he开头,忽略大小写
56         NSPredicate *predicate6 = [NSPredicate predicateWithFormat:@"self beginswith[c] 'he'"];
57         resultArr = [strArray filteredArrayUsingPredicate:predicate6];
58         NSLog(@"beginswith%@", resultArr);
59         // 包含o的
60         NSPredicate *predicate7 = [NSPredicate predicateWithFormat:@"self contains 'o'"];
61         resultArr = [strArray filteredArrayUsingPredicate:predicate7];
62         NSLog(@"contains%@", resultArr);
63         
64 #pragma mark LIKE运算符
65         NSPredicate *predicate8 = [NSPredicate predicateWithFormat:@"self like[c] 'h*'"];
66         resultArr = [strArray filteredArrayUsingPredicate:predicate8];
67         NSLog(@"like %@", resultArr);
68         
69 #pragma mark 正则表达式
70         // 验证邮箱
71         NSString *str = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
72         NSPredicate *predicate9 = [NSPredicate predicateWithFormat:@"self matches %@", str];
73         if ([predicate9 evaluateWithObject:@"http://www.ba"]) {
74             NSLog(@"正确");
75         } else {
76             NSLog(@"错误");
77         }
78     }
79     return 0;
80 }
复制代码
posted @ 2016-05-24 17:10  董小火  阅读(136)  评论(0编辑  收藏  举报