NSPredicate的一些简单用法

NSPredicate可以支持数据库查询,平时的一些数组查询之类也用这个方法,以下是一些总结

格式字符串的一些基本写法

1> @"attributeName == %@" 就是属性名字等于某个值
2>@"%K == %@",将属性名字变为%K,增加拓展性.

// 1.第一种写法,右边值如果是字符串,可以用单引号表示
NSPredicate *pre = [NSPredicate predicateWithFormat:@"nickName == '小明'",];
// 2.第二种写法,%K的K必须大写,不能用%@代替
NSString *name = @"cat.nickName";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"%K == %@",name,@"小明"];

左右两边基本的比较操作符号

1.=,==左边表达式等于右手表达式

2.>=,=>左边表达式大于或等于右边表达式

3.<=,=<左变表达式小于或等于右边表达式

4> 或者 <

5.!=,<>左边表达式不等于右边表达式

6.IN左边表达式必须出现在右边表达式指定的集合中。即name IN {'Milk','Eggs','Bread'}

7.BETWEEN左边表达式在右边表达式之间或等于右边表达式。即1 {0,33}之间。如果你的左边表达式是0或33,也是真的

8.以下是字符串表达式的比较符号
(1)BEGINSWITH左边表达式以右边表达式开始
(2)CONTAINS左边表达式包含表达式
(3)ENDSWITH左边表达式以右边表达式结束
(4)LIKE左边表达式等于右手表达式:*作为通配符,其中匹配1个字符,*匹配0个或多个字符
(5)MATCHES左边表达式等于右边表达式使用正则表达式样式比较

两个表达式的逻辑符号

1.AND,&&,逻辑AND

2.OR,||逻辑或

3.NOT,!逻辑NOT

数组的操作的一些特性

1.对数组中数字集合的一些操作
(1)@avg返回collection中对象平均值,以NSNumber的形式返回
(2)@count集合中总共的个数,,以NSNumber的形式返回
(3)@min````@max````@sum分别表示返回集合中最小值,最大值,和总和.都是以NSNumber的形式返回.
(4)使用valueForKeyPath返回相应的结果

// 处理大量数字组成的数组的时候可以使用,可以方便的进行判断.然后进行下一步操作
NSArray *array = @[@(30),@(40),@(50)];
NSPredicate *pre = [NSPredicate
// 最小值是不是大于30
predicateWithFormat:@"@min.intValue > 30"];
BOOL ok = [pre evaluateWithObject:array];
// 可以使用key == value 来去的最小,最大,总和,平均等值.并且返回一个nsnumer类型的值
NSNumber *a =  [array11 valueForKeyPath:@"@min.intValue"];
NSLog(@"%d",a.intValue);

2.数组中存放对象的一些操作
(1) @distinctUnionOfObjects返回一个数组,这个数组是由操作符.右侧属性的值组成的,并且重复的值被过滤掉.@unionOfObjects逻辑和@distinctUnionOfObjects相同,但是重复的值没有被过滤掉
(2)@distinctUnionOfArrays返回的是数组,但是将数组中的数组元素拆分到一个数组里面,逻辑同上面,不包括重复值@unionOfArrays包括重复值
(3)返回值都是使用valueForKeyPath取到

CatModal *cat1 = [[CatModal alloc] initWithNickName:@"小明" withWeight:30.1];
CatModal *cat2 = [[CatModal alloc] initWithNickName:@"mimi" withWeight:33.1];

CatModal *cat4 = [[CatModal alloc] initWithNickName:@"fuu" withWeight:35.1];
CatModal *cat5 = [[CatModal alloc] initWithNickName:@"err" withWeight:35.1];

TestModal *test1 = [[TestModal alloc] initWithName:@"小七" withHeith:90.1 withCat:cat1];
TestModal *test2 = [[TestModal alloc] initWithName:@"小七" withHeith:80.1 withCat:cat2];
withHeith:60.1 withCat:cat3];

TestModal *test4 = [[TestModal alloc] initWithName:@"小七" withHeith:60.1 withCat:cat4];

TestModal *test5 = [[TestModal alloc] initWithName:@"小七" withHeith:60.1 withCat:cat5];

NSArray *array = @[test2,test1];
NSArray *array2 = @[test4,test5];
NSArray *arrayofarray = @[array,array2];


NSArray *array11111 = [array valueForKeyPath:@"@distinctUnionOfObjects.cat"];
for (CatModal *cat in array11111) {
    NSLog(@"%@\n",cat.nickName);[小明,mini]
}


NSArray *array1 = [arrayofarray valueForKeyPath:@"@distinctUnionOfArrays.cat"];
for (CatModal *cat in array1) {
    NSLog(@"%@\n",cat.nickName); // [fuu 小明 mimi err],并不是数组中的数组的形式
}

3.数组中的SELF指什么
数组中SELF指的是数组中包含的每一个对象

4.对数组中具体的某个位置进行操作查询

array[index]指定数组中指定索引处的元素进行匹配操作
array[FIRST]````array[LAST]````array[SIZE]分别表示对数组第一个,最后一个,数组总数进行操作

CatModal *cat1 = [[CatModal alloc] initWithNickName:@"小明" withWeight:30.1];
CatModal *cat2 = [[CatModal alloc] initWithNickName:@"mimi" withWeight:33.1];
CatModal *cat3 = [[CatModal alloc] initWithNickName:@"miru" withWeight:35.1];
CatModal *cat4 = [[CatModal alloc] initWithNickName:@"fuu" withWeight:35.1];
CatModal *cat5 = [[CatModal alloc] initWithNickName:@"err" withWeight:35.1];
CatModal *cat6 = [[CatModal alloc] initWithNickName:@"ooo" withWeight:35.1];
TestModal *test1 = [[TestModal alloc] initWithName:@"小七" withHeith:90.1 withCat:cat1];
test1.catsArray = @[cat2,cat1];

TestModal *test2 = [[TestModal alloc] initWithName:@"小七" withHeith:80.1 withCat:cat2];
test2.catsArray = @[cat4];
TestModal *test3 = [[TestModal alloc] initWithName:@"小七" withHeith:60.1 withCat:cat3];
test3.catsArray = @[cat5,cat6];

NSArray *array = @[test2,test1,test3];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"catsArray[SIZE] == 1"];
NSArray *array1 = [array filteredArrayUsingPredicate:pre];
NSLog(@"11111%@",array1);

5.对数组中的数组进行操作的时候查询关键字,只能用在数组,一对多的形式
ANY或者SOME对数组中的数组进行查询,只要有符合条件的,就返回TRUE

ALL数组中所有都符合条件的时候才会返回。

NONE都不正确的时候才返回true。

CatModal *cat1 = [[CatModal alloc] initWithNickName:@"小明" withWeight:30.1];
CatModal *cat2 = [[CatModal alloc] initWithNickName:@"mimi" withWeight:33.1];
CatModal *cat3 = [[CatModal alloc] initWithNickName:@"miru" withWeight:35.1];
TestModal *test1 = [[TestModal alloc] initWithName:@"小七" withHeith:90.1 withCat:cat1];
test1.catsArray = @[cat2,cat3];

TestModal *test2 = [[TestModal alloc] initWithName:@"小七" withHeith:80.1 withCat:cat1];
test2.catsArray = @[cat2];
TestModal *test3 = [[TestModal alloc] initWithName:@"小七" withHeith:60.1 withCat:cat1];
test3.catsArray = @[cat1,cat3];
NSArray *array = @[test2,test1,test3];
// ALL ANY SOME 用于NSArray NSSet .ANY SOME是数组中有真的就返回. ALL是数组中全部是真的才返回
NSPredicate *pre = [NSPredicate predicateWithFormat:@"ALL catsArray.weight > 31.0"];
NSArray *array1 = [array filteredArrayUsingPredicate:pre];
NSLog(@"%@",array1);

注意:
1.查询过程中使用department.name这样的形式比较耗费性能.尽量不要使用.department == %@这样的形式查询效率最高.

  1. @"firstName beginswith[cd] 'Matt' AND (ANY directreports.paygrade <= 7)" 比下面这种形式更高效 @"(ANY directreports.paygrade <= 7) AND (firstName beginswith[cd] 'Matt')"
    3.使用predicate的类必须支持key-value-coding ,这样才可以在其他地方使用. 实际上使用predicate 也是根据key-value这样的形式来查询的.

4.[c],[d],[cd]可以加到比较符号的后面表示不区分大小写,不区分发音符号,两这个都不区分

5.如果想要匹配null或者nil值得时候,需要额外增加一条对比

predicate = [NSPredicate predicateWithFormat:@"(firstName == %@) || (firstName == nil)"];
filteredArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filteredArray);

你可能感兴趣的:(NSPredicate的一些简单用法)