NSPredicate简述
前言
NSPredicate是一个逻辑条件的定义,这个逻辑条件用来约束一个搜索条件,而这个搜索条件用于数据的获取或内存中数据的过程。仅作于笔记,如有侵权,联系删。
正文
一、过滤数值
1.判断数值相等,大小等
NSNumber *num0 = @123;
NSNumber *num1 = @1234;
NSString *str0 = @"123";
NSPredicate *pre0 = [NSPredicate predicateWithFormat:@"SELF = 123"];
NSLog(@"%d",[pre0 evaluateWithObject:num0]);//1
NSLog(@"%d",[pre0 evaluateWithObject:num1]);//0
NSLog(@"%d",[pre0 evaluateWithObject:str0]);//0
NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"SELF > 1000"];
NSLog(@"%d",[pre1 evaluateWithObject:num0]);//1
NSLog(@"%d",[pre1 evaluateWithObject:num1]);//0
2.判断数值范围Between
NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF BETWEEN {100, 200}"];
NSLog(@"%d",[pre2 evaluateWithObject:num0]);//1
NSLog(@"%d",[pre2 evaluateWithObject:num1]);//0
二、过滤字符串
1.BEGINWITH:是否以指定字符串开头
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'a'"];
NSLog(@"%d",[pre evaluateWithObject:@"ab"]);//1
NSLog(@"%d",[pre evaluateWithObject:@"abc"]);//1
NSLog(@"%d",[pre evaluateWithObject:@"ba"]);//0
2.ENDSWITH:是否以指定字符串结尾
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF ENDSWITH 'ba'"];
NSLog(@"%d",[pre evaluateWithObject:@"ab"]);//0
NSLog(@"%d",[pre evaluateWithObject:@"abc"]);//0
NSLog(@"%d",[p