iOS中的谓词
2016-04-08 00:00
337 查看
谓词(保留所判断的,筛选用)
第一种使用谓词筛选的方法:
1
2
3
第二种使用谓词筛选的方法(使用数组,比较简便):
例子:谓词
1
2
3
设置谓词
第一种使用谓词筛选的方法:
1
#import <Foundation/Foundation.h> @interface man : NSObject //第一步:新建一个man类,里面有name和age两种属性 @property (nonatomic,strong)NSString *name; @property (nonatomic,assign)int age; @end
2
#import "man.h" @implementation man //第二步:要打印对象的时候,其实也是用的description方法,这里我们要把它写出来,就可以看出打印的数据了,否则会是乱码 -(NSString *)description{ return [NSString stringWithFormat:@"%@ %d", self.name,self.age]; } @end
3
#import "ViewController.h" //注意引入类man.h头文件 #import "man.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //第三步:创建一个数组,里面存有10个不同的属性 NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10]; for (int i=0; i<10; i++) { //使用类man创建一个m属性,注意引入头文件 man *m = [[man alloc]init]; if (i<5) { //当循环变量i小于5的时候,m属性的名字叫qiang+i m.name = [NSString stringWithFormat:@"qiang%d", i]; }else{ //当循环变量i大于5的时候,m属性的名字叫hong+i m.name = [NSString stringWithFormat:@"hong%d", i]; } //m的年龄是20+i m.age = 20+i; //最后将m属性添加到数组a中 [a addObject:m]; } //创建一个谓词b,筛选条件是年龄小于25,谓词筛选条件可以是(&& || and等等,好多好多) NSPredicate *b = [NSPredicate predicateWithFormat:@"age<25"]; //使用类man新建一个属性x,遍历数组a for (man *x in a) { //使用谓词b来筛选每一个遍历过的x值 BOOL yesOrNo = [b evaluateWithObject:x]; //判断并打印x值 if (yesOrNo) { NSLog(@"%@", x); } } }
第二种使用谓词筛选的方法(使用数组,比较简便):
#import "ViewController.h" //注意引入类man.h头文件 #import "man.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //第三步:创建一个数组,里面存有10个不同的属性 NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10]; for (int i=0; i<10; i++) { //使用类man创建一个m属性,注意引入头文件 man *m = [[man alloc]init]; if (i<5) { //当循环变量i小于5的时候,m属性的名字叫qiang+i m.name = [NSString stringWithFormat:@"qiang%d", i]; }else{ //当循环变量i大于5的时候,m属性的名字叫hong+i m.name = [NSString stringWithFormat:@"hong%d", i]; } //m的年龄是20+i m.age = 20+i; //最后将m属性添加到数组a中 [a addObject:m]; } //创建一个谓词b,筛选条件是年龄小于25,谓词筛选条件可以是(&& || and等等,好多好多) NSPredicate *b = [NSPredicate predicateWithFormat:@"age<25"]; //第二种比较简便的方法:使用数组方法来筛选,不用for循环(在数组a中使用谓词b来筛选,将结果存到数组c中) NSArray *c = [a filteredArrayUsingPredicate:b]; NSLog(@"%@", c); }
例子:谓词
1
#import <Foundation/Foundation.h> @interface student : NSObject //第一步:先创建一个类studen,里面有两个属性name和age @property (nonatomic,copy)NSString *name; @property (nonatomic,assign)NSInteger age; //第二步:给类student分配内存空间和赋值(方法声明) +(student *)allocWithname:(NSString *)name withAge:(NSInteger)age; @end
2
#import "student.h" @implementation student //第三步:给类student分配内存空间和赋值(方法实现) +(student *)allocWithname:(NSString *)name withAge:(NSInteger)age{ student *s = [[student alloc]init]; s.name = name; s.age = age; return s; } //第四步:如果想看到打印后的结果,就要写这个description方法,否则打印出来是乱码 -(NSString *)description{ NSString *n = [[NSString alloc] initWithFormat:@"age:%ld name:%@", _age, _name]; return n; } @end
3
#import "ViewController.h" //引入类student.h的头文件 #import "student.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //第五步:分别新建一个姓名库和年龄库的数组,存储各自的信息 NSArray *names = @[@"xiaoming", @"xiaohong", @"xiaoqiang", @"xiaogang", @"xiaoliu", @"xiaowang", @"xiaoma", ]; NSArray *ages= @[@20, @25, @30, @35, @40, @45, @50]; //新建一个数组stuArr NSMutableArray *stuArr = [NSMutableArray array]; //遍历names数组(ages也一样)的成员个数 for (int i=0; i<names.count; i++) { //使用类student创建一个临时成员stu,用来接收存储names数组和ages数组中的信息 student *stu = [student allocWithname:names[i] withAge:[ages[i]integerValue]]; //将stu添加到数组stuArr中 [stuArr addObject:stu]; } //第六步:新建一个谓词a,设置筛选条件 NSPredicate *a = [NSPredicate predicateWithFormat:@"age<35"]; //在数组stuArr中使用谓词a来筛选,将结果存到数组b中 NSArray *b = [stuArr filteredArrayUsingPredicate:a]; NSLog(@"%@", b); }
设置谓词
#import "ViewController.h" #import "people.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *a = [[NSMutableArray alloc]initWithCapacity:10]; for (int i=0; i<10; i++) { people *p1 = [[people alloc]init]; if (i<5) { p1.name = [NSString stringWithFormat:@"qiang%d", i]; }else{ p1.name = [NSString stringWithFormat:@"hong%d", i]; } p1.age = 20+i; [a addObject:p1]; } NSLog(@"%@", a); //-----------------设置谓词(运算符)--------------------- //小于 NSPredicate *b = [NSPredicate predicateWithFormat:@"age<27"]; //与 条件使用 NSPredicate *b1 = [NSPredicate predicateWithFormat:@"age<27 && age>23"]; NSPredicate *b2 = [NSPredicate predicateWithFormat:@"age<27 and age>23"]; //或 条件使用 NSPredicate *b3 = [NSPredicate predicateWithFormat:@"age<24 || age>27"]; NSPredicate *b4 = [NSPredicate predicateWithFormat:@"age<24 or age>27"]; //给占位符号传值方法 NSPredicate *b5 = [NSPredicate predicateWithFormat:@"age<%d", 25]; //等于条件(保留name = 'hong5) NSPredicate *b6 = [NSPredicate predicateWithFormat:@"name = 'hong5'"]; //in条件使用 NSPredicate *b7 = [NSPredicate predicateWithFormat:@"name in {'qiang3','qiang4','hong5'}"]; //直接传递数组 NSArray *x = [NSArray arrayWithObjects:@"qiang4", @"hong5", @"hong6", nil]; NSPredicate *b8 = [NSPredicate predicateWithFormat:@"name in %@", x]; //-----------------设置谓词(关键字)--------------------- //以什么开始设置(首字母) NSPredicate *b9 = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'q'", x]; //以什么结束设置(尾字母) NSPredicate *b10 = [NSPredicate predicateWithFormat:@"name ENDSWITH '7'", x]; //包含什么 NSPredicate *b11 = [NSPredicate predicateWithFormat:@"name CONTAINS 'o'"]; //模糊匹配 NSPredicate *b12 = [NSPredicate predicateWithFormat:@"name LIKE '*i*'"]; //外部传入的写法 NSString *j=[NSString stringWithFormat:@"name LIKE '*%@*'",@"o"]; NSPredicate *b13 = [NSPredicate predicateWithFormat:j]; NSArray *c = [a filteredArrayUsingPredicate:b13]; NSLog(@"ddddddd%@", c); }
相关文章推荐
- iOS中添加逻辑流程(把创建的导航添加到window的rootview)
- iOS之简单瀑布流的实现
- iOS搜索框的实现
- IOS 内存管理机制总结
- iOS推送后页面跳转
- ios block和函数的区别
- IOS中的数据存储方式,特点,使用情况
- IOS中的数据存储方式,特点,使用情况
- iOS 保持界面流畅的技巧
- 封装.a文件以及应用.a文件
- iOS AFNetWorking中block执行完后再执行其它操作
- 浅谈iOS模糊效果
- NSString 去除空格方法 ios
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- [IOS]动画效果
- iOS开发中一些常用的check事项2
- ios开发中的一些注册流程的Check
- IOS开发基础知识--碎片36
- 个人偏好设置,归档,解档
- [iOS]关于横屏后状态栏不显示的问题