//
// main.m
// NSPredicateDemo
//
// Created by qingyun on 15/12/18.
// Copyright (c) 2015年 listen. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//用来查询和过滤 类似于SQL数据库中的where
NSArray *array=@[@111,@323,@35,@345,@67];
NSLog(@"原始数组:%@",array);
//1.创建一个谓词对象:指定筛选条件
NSLog(@"\n================谓词使用运算符================");
//过滤数组大于100的对象
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF>100"];//SELF本身代表被过滤的数组中的每一个对象本身 支持所有的关系运算符(> >= < <= == !=)
NSArray *fltarray=[array filteredArrayUsingPredicate:predicate];
NSLog(@"数组中大于100的数字有:%@",fltarray);
//评估单个对象
NSNumber *num=@234;
BOOL reesult=[predicate evaluateWithObject:num];//num是否满足之前定义的条件
NSLog(@"%d",reesult);
NSLog(@"\n================谓词对数组的使用================");
//创建Person类对象的数组
Person *p1=[[Person alloc]initWithName:@"listen" andAge:20];
Person *p2=[[Person alloc]initWithName:@"to" andAge:21];
Person *p3=[[Person alloc]initWithName:@"rain" andAge:22];
Person *p4=[[Person alloc]initWithName:@"lgeg" andAge:23];
Person *p5=[[Person alloc]initWithName:@"bao" andAge:24];
Person *p6=[[Person alloc]initWithName:@"bao1" andAge:24];
NSArray *personArray=@[p1,p2,p3,p4,p5,p6];
NSLog(@"过滤之前的:%@",personArray);
//谓词是基于KVC机制的 可以通过属性对应的字符串来标示想要访问的属性
predicate=[NSPredicate predicateWithFormat:@"self.name=='listen'"];
//对数组施加谓词规则
NSLog(@"\n================对数组施加谓词规则================");
NSArray *fltPerson=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson满足条件的是:%@",fltPerson);
predicate=[NSPredicate predicateWithFormat:@"self.age<22"];
NSArray *fltPerson1=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson1满足条件的是:%@",fltPerson1);
//同时满足多个条件 支持逻辑与 或 非(&&==and)(||==OR)(!==not)
NSLog(@"\n================同时满足多个条件(与或非)================");
predicate=[NSPredicate predicateWithFormat:@"self.name=='listen'||self.age>22"];
NSArray *fltPerson2=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson2满足条件的是:%@",fltPerson2);
predicate=[NSPredicate predicateWithFormat:@"!self.name=='listen'&&self.age<22"];
NSArray *fltPerson3=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson3满足条件的是:%@",fltPerson3);
//访问对象属性时 self. 可以省略 但是如果是用对象本身的值(比如NSNumber类的对象)时,要用SELF
//BEGINSWITH (以xxxx开头) ENDsWITH (以xxx结尾) CONTAINS (包含xxx)
//[cd]中的c是不区分大小写 d是不区分重音符号
NSLog(@"\n================BEGINSWITH ENDsWITH CONTAINS================");
predicate=[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'l'"];
NSArray *fltPerson4=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson4满足条件的是:%@",fltPerson4);
predicate=[NSPredicate predicateWithFormat:@"name endswith 'o'"];
NSArray *fltPerson5=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson5满足条件的是:%@",fltPerson5);
predicate=[NSPredicate predicateWithFormat:@"name contains 'e'"];
NSArray *fltPerson6=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson6满足条件的是:%@",fltPerson6);
//IN(指定离散的范围 只和{}中的值匹配) BETWEEN(指定一个闭区间范围{num1,num2}表示num1~num2之间的值)
NSLog(@"\n================取范围================");
predicate=[NSPredicate predicateWithFormat:@"age IN {22,24}"];
NSArray *fltPerson7=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson7满足条件的是:%@",fltPerson7);
predicate=[NSPredicate predicateWithFormat:@"age between {22,24}"];
NSArray *fltPerson8=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson8满足条件的是:%@",fltPerson8);
//ALL(所有的都满足) ANY(至少有一个满足) 施加的是集合对象
NSLog(@"\n================all和any================");
predicate =[NSPredicate predicateWithFormat:@"all age>22"];
BOOL result1=[predicate evaluateWithObject:personArray];
if (result1) {
NSLog(@"所有人年龄都大于22");
}
else
{
NSLog(@"有不满二十岁的");
}
predicate =[NSPredicate predicateWithFormat:@"any age>22"];
BOOL result2=[predicate evaluateWithObject:personArray];
if (result2) {
NSLog(@"至少有一个人大于22");
}
else
{
NSLog(@"所有的人都不满二十二岁");
}
NSLog(@"\n================谓词占位符================");
//%k是key的占位符 只能在谓词中使用
NSString *key=@"name";
NSString *value=@"lgeng";
predicate =[NSPredicate predicateWithFormat:@"%K==%@",key,value];
NSArray *fltPerson9=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson9满足条件的是:%@",fltPerson9);
//谓词中使用变量($NAME)
NSLog(@"\n================谓词模板================");
//先生成一个谓词模板
NSPredicate *predicateTempLate=[NSPredicate predicateWithFormat:@"name==$NAME"];
//由谓词模板生成谓词
//1.创建一个字典
NSString *name=@"listen";
NSDictionary *dict=@{@"NAME":name};
//2.生成一个具体的谓词 用字典中的Value 来取代模板的变量
predicate =[predicateTempLate predicateWithSubstitutionVariables:dict];
NSArray *fltPerson10=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson10满足条件的是:%@",fltPerson10);
predicateTempLate=[NSPredicate predicateWithFormat:@"age between$RANGE"];
NSNumber *beginAge=@20;
NSNumber *endAge=@23;
dict=@{@"RANGE":@[beginAge,endAge]};
predicate=[predicateTempLate predicateWithSubstitutionVariables:dict];
NSArray *fltPerson11=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson11满足条件的是:%@",fltPerson11);
//谓词中使用通配符 like (*匹配多个字符)(?匹配单个字符 但是可以叠加??就是两个字符)
predicate=[NSPredicate predicateWithFormat:@"name like'l*'"];
NSArray *fltPerson12=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson12满足条件的是:%@",fltPerson12);
predicate=[NSPredicate predicateWithFormat:@"name like't?'"];
NSArray *fltPerson13=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson13满足条件的是:%@",fltPerson13);
predicate=[NSPredicate predicateWithFormat:@"name like'?o'"];
NSArray *fltPerson14=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson14满足条件的是:%@",fltPerson14);
predicate=[NSPredicate predicateWithFormat:@"name like'*n'"];
NSArray *fltPerson15=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson15满足条件的是:%@",fltPerson15);
//正则表达式matches
NSLog(@"\n================谓词正则表达式================");
predicate =[NSPredicate predicateWithFormat:@"name matches '^l.*n$'"];
NSArray *fltPerson16=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson16满足条件的是:%@",fltPerson16);
predicate =[NSPredicate predicateWithFormat:@"name matches '^ra.?n$'"];
NSArray *fltPerson17=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson17满足条件的是:%@",fltPerson17);
predicate =[NSPredicate predicateWithFormat:@"name matches '^..*[1]$'"];
NSArray *fltPerson18=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson18满足条件的是:%@",fltPerson18);
predicate =[NSPredicate predicateWithFormat:@"name matches '^.*$'"];
NSArray *fltPerson19=[personArray filteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson19满足条件的是:%@",fltPerson19);
}
return 0;
}
打印结果
原始数组:(
111,
323,
35,
345,
67
)
================谓词使用运算符================
数组中大于100的数字有:(
111,
323,
345
)
1
================谓词对数组的使用================
过滤之前的:(
"name:listen age:20",
"name:to age:21",
"name:rain age:22",
"name:lgeg age:23",
"name:bao age:24",
"name:bao1 age:24"
)
================对数组施加谓词规则================
fltPerson满足条件的是:(
"name:listen age:20"
)
fltPerson1满足条件的是:(
"name:listen age:20",
"name:to age:21"
)
================同时满足多个条件(与或非)================
fltPerson2满足条件的是:(
"name:listen age:20",
"name:lgeg age:23",
"name:bao age:24",
"name:bao1 age:24"
)
fltPerson3满足条件的是:(
"name:to age:21"
)
================BEGINSWITH ENDsWITH CONTAINS================
fltPerson4满足条件的是:(
"name:listen age:20",
"name:lgeg age:23"
)
fltPerson5满足条件的是:(
"name:to age:21",
"name:bao age:24"
)
fltPerson6满足条件的是:(
"name:listen age:20",
"name:lgeg age:23"
)
================取范围================
fltPerson7满足条件的是:(
"name:rain age:22",
"name:bao age:24",
"name:bao1 age:24"
)
fltPerson8满足条件的是:(
"name:rain age:22",
"name:lgeg age:23",
"name:bao age:24",
"name:bao1 age:24"
)
================all和any================
有不满二十岁的
至少有一个人大于22
================谓词占位符================
fltPerson9满足条件的是:(
)
================谓词模板================
fltPerson10满足条件的是:(
"name:listen age:20"
)
fltPerson11满足条件的是:(
"name:listen age:20",
"name:to age:21",
"name:rain age:22",
"name:lgeg age:23"
)
fltPerson12满足条件的是:(
"name:listen age:20",
"name:lgeg age:23"
)
fltPerson13满足条件的是:(
"name:to age:21"
)
fltPerson14满足条件的是:(
"name:to age:21"
)
2015-12-18 17:55:41.463 NSPredicateDemo[1062:658668] fltPerson15满足条件的是:(
"name:listen age:20",
"name:rain age:22"
)
================谓词正则表达式================
fltPerson16满足条件的是:(
"name:listen age:20"
)
fltPerson17满足条件的是:(
"name:rain age:22"
)
fltPerson18满足条件的是:(
"name:bao1 age:24"
)
fltPerson19满足条件的是:(
"name:listen age:20",
"name:to age:21",
"name:rain age:22",
"name:lgeg age:23",
"name:bao age:24",
"name:bao1 age:24"
)