C/C++教程

OC语言 - NSPredicate

本文主要是介绍OC语言 - NSPredicate,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

NSPredicate

1 - NSPredicate 主要用来指定过滤器的条件:该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配

2 - 谓词是指在计算机中表示计算真假值的函数,原理和用法都类似于 SQL 查询中的 where,作用相当于数据库的过滤取。主要用于从集合中分拣出符合条件的对象,也可以用于字符串的正则匹配。起始谓词就是判断一个对象是否符合某个过滤条件,返回 bool 类型,如果是一系列的对象那么就使用一个循环

3 - 常规 API

① 比较运算符 > 、< 、== 、 >= 、<= 、!=

@"number >= 99"

② 逻辑运算符:AND、OR、NOT,这几个运算符计算并、或、非的结果

③ 范围运算符:IN 、BETWEEN

@"number BETWEEN {1,5}";
@"address IN {'shanghai','nanjing'}"

④ 字符串本身:SELF

@"SELF == 'APPLE'"

⑤ 字符串相关:BEGINSWITH、ENDSWITH、CONTAINS

@"name CONTAIN[cd] 'ang'"   // 包含某个字符串
@"name BEGINSWITH[c] 'sh'"  // 以某个字符串开头
@"name ENDSWITH[d] 'ang'"   // 以某个字符串结束

注:[c] 不区分大小写 ; [d] 不区分发音符号即没有重音符号 ;[cd]既不区分大小写、也不区分发音符号

⑥ 通配符:LIKE

@"name LIKE[cd] '*er*'" // 代表通配符,Like 也接受 [cd]

⑦ 正则表达式:MATCHES

NSString *regex = @"^A.+e$";// 以 A 开头,并以 e 结尾
@"name MATCHES %@",regex

4 - 代码示例:新建 Person 文件充当数据

// - Person.h

1 #import <Foundation/Foundation.h>
2 @interface Person : NSObject
3 
4 @property (nonatomic, copy)NSString *name;
5 @property (nonatomic ,assign)NSInteger age;
6 
7 @end

// - Person.m

1 #import "Person.h"
2 @implementation Person
3 - (NSString *)description{
4     
5     return [NSString stringWithFormat:@"name= %@ sex = %ld", _name, _age];
6 }
7 
8 @end

// - main.m

  1 #import <Foundation/Foundation.h>
  2 #import "Person.h"
  3 int main(int argc, const char * argv[]) {
  4     
  5     NSMutableArray * array = [NSMutableArray arrayWithCapacity:1];
  6     
  7     // 遍历几条数据
  8     for (int i = 0; i < 10; i++) {
  9         
 10         Person * per = [[Person alloc] init];
 11         
 12         // 配置姓名
 13         if (i < 5) {
 14             per.name = [NSString stringWithFormat:@"jack-%d",i];
 15         }
 16         else{
 17             per.name = [NSString stringWithFormat:@"tom-%d",i];
 18         }
 19         // 配置年龄
 20         per.age = 20 + i;
 21         [array addObject:per];
 22     }
 23     
 24     
 25     
 26     // 过滤年龄
 27     NSPredicate *predict01 = [NSPredicate predicateWithFormat:@"age < %d", 25];
 28     NSLog(@"%@",predict01);// 输出:age < 25
 29     // 开始过滤
 30     for (Person *per in array) {
 31 
 32         BOOL result = [predict01 evaluateWithObject:per];
 33         if (result) {
 34             NSLog(@"%@",per);
 35         }
 36     }
 37     
 38     NSLog(@"~~~~~~~~~~~~~~~~ 或 ~~~~~~~~~~~~~~~~~~~");
 39     // 或
 40     NSPredicate * predict02 = [NSPredicate predicateWithFormat:@"name == 'jack-0' || name == 'jack-4'"];
 41     NSLog(@"%@",predict02);
 42     for (Person * per in array) {
 43         BOOL result = [predict02 evaluateWithObject:per];
 44         if (result) {
 45             NSLog(@"%@",per);
 46         }
 47     }
 48     NSLog(@"~~~~~~~~~~~~~~ 范围运算 ~~~~~~~~~~~~~~~");
 49    // 范围运算
 50     NSPredicate * predict03 = [NSPredicate predicateWithFormat:@"name IN {'jack-0','jack-2', 'tom-6', 'tom-8'}"];
 51     NSLog(@"%@",predict03);
 52     for (Person * per in array) {
 53         BOOL result = [predict03 evaluateWithObject:per];
 54         if (result) {
 55             NSLog(@"%@",per);
 56         }
 57     }
 58     
 59     NSLog(@"~~~~~~~~~~~~~~~ beginswith、endswith、contains~~~~~~~~~~~~~~~~~");
 60     // beginswith、endswith、contains
 61     NSPredicate *predict04 = [NSPredicate predicateWithFormat:@"name beginswith 'j'"];
 62     NSLog(@"%@",predict04);
 63     for (Person *per in array) {
 64         BOOL result = [predict04 evaluateWithObject:per];
 65         if (result) {
 66             NSLog(@"%@",per);
 67         }
 68     }
 69 
 70     NSPredicate *predict05 = [NSPredicate predicateWithFormat:@"name contains 'o'"];
 71     NSLog(@"%@",predict05);
 72     for (Person *per in array) {
 73         BOOL result = [predict05 evaluateWithObject:per];
 74         if (result) {
 75             NSLog(@"%@",per);
 76         }
 77     }
 78     
 79     NSLog(@"~~~~~~~~~~~~~~ like ~~~~~~~~~~~~~~~~~~");
 80     // like
 81     //NSPredicate * predicate06 = [NSPredicate predicateWithFormat:@"self.name like '*jack*'"];
 82     NSPredicate * predicate06 = [NSPredicate predicateWithFormat:@"self.name like 'jack*'"];
 83     NSLog(@"%@",predicate06);
 84     for (Person *per in array) {
 85         BOOL result = [predicate06 evaluateWithObject:per];
 86         if (result) {
 87 
 88             NSLog(@"%@",per);
 89         }
 90     }
 91     
 92     NSLog(@"~~~~~~~~~~~~~ 以 t 开头并以 5 结尾 ~~~~~~~~~~~~~~~");
 93 
 94     // 以 t 开头并以 5 结尾
 95     NSString * regex = @"^t.+5$";
 96     NSPredicate *predicate07 = [NSPredicate predicateWithFormat:@"self.name matches %@", regex];
 97     NSLog(@"%@",predicate07);
 98     for (Person * per in array){
 99         BOOL result = [predicate07 evaluateWithObject:per];
100         if (result) {
101             NSLog(@"%@",per);
102         }
103     }
104     
105     NSLog(@"~~~~~~~~~~~ 两数组交集 ~~~~~~~~~~~~");
106 
107     // 两数组交集
108     NSArray *array1 = [NSArray arrayWithObjects:@"abc1", @"abc2", nil];
109     NSArray *array2 = [NSArray arrayWithObjects:@"a1", @"abc1", @"abc4", @"abc2", nil];
110     NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", array1];
111     NSArray *resultArray = [array2 filteredArrayUsingPredicate:thePredicate];
112     NSLog(@"%@",resultArray);
113     
114     NSLog(@"~~~~~~~~~~~ 过滤数组 ~~~~~~~~~~~~~~");
115     
116     // 过滤数组
117     NSArray *arrayFilter = [NSArray arrayWithObjects:array[0], array[3], nil];
118     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", arrayFilter];
119     // 可变数组过滤没有返回值;不可变数组过滤返回不可变数组
120     // [array filterUsingPredicate:predicate];
121     // NSLog(@"%@",array);
122 
123     // 遍历出符合过滤条件的对象
124     NSArray *arrayFilter01 = [NSArray arrayWithObjects:array[0], array[3], nil];
125     for (int i = 0; i < arrayFilter01.count; i++) {
126         
127         Person * filterPerson = [arrayFilter01 objectAtIndex:i];
128         NSPredicate * predicate = [NSPredicate predicateWithFormat:@"self.name contains %@", filterPerson.name];
129         [array filterUsingPredicate:predicate];// 要注掉上行过滤数组语句,否则 array 过滤后内容已改变,执行崩溃
130         NSLog(@"%@",filterPerson);
131     }
132     
133     return 0;
134 }

日志打印

这篇关于OC语言 - NSPredicate的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!