objective-c之谓词
来源:互联网 发布:功夫熊猫中的师傅知乎 编辑:程序博客网 时间:2024/04/29 03:31
//
// 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=[NSPredicatepredicateWithFormat:@"SELF>100"];//SELF本身代表被过滤的数组中的每一个对象本身支持所有的关系运算符(> >= < <= == !=)
NSArray *fltarray=[arrayfilteredArrayUsingPredicate:predicate];
NSLog(@"数组中大于100的数字有:%@",fltarray);
//评估单个对象
NSNumber *num=@234;
BOOL reesult=[predicateevaluateWithObject:num];//num是否满足之前定义的条件
NSLog(@"%d",reesult);
NSLog(@"\n================谓词对数组的使用================");
//创建Person类对象的数组
Person *p1=[[Personalloc]initWithName:@"listen"andAge:20];
Person *p2=[[Personalloc]initWithName:@"to"andAge:21];
Person *p3=[[Personalloc]initWithName:@"rain"andAge:22];
Person *p4=[[Personalloc]initWithName:@"lgeg"andAge:23];
Person *p5=[[Personalloc]initWithName:@"bao"andAge:24];
Person *p6=[[Personalloc]initWithName:@"bao1"andAge:24];
NSArray *personArray=@[p1,p2,p3,p4,p5,p6];
NSLog(@"过滤之前的:%@",personArray);
//谓词是基于KVC机制的 可以通过属性对应的字符串来标示想要访问的属性
predicate=[NSPredicatepredicateWithFormat:@"self.name=='listen'"];
//对数组施加谓词规则
NSLog(@"\n================对数组施加谓词规则================");
NSArray *fltPerson=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson满足条件的是:%@",fltPerson);
predicate=[NSPredicatepredicateWithFormat:@"self.age<22"];
NSArray *fltPerson1=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson1满足条件的是:%@",fltPerson1);
//同时满足多个条件 支持逻辑与 或 非(&&==and)(||==OR)(!==not)
NSLog(@"\n================同时满足多个条件(与或非)================");
predicate=[NSPredicatepredicateWithFormat:@"self.name=='listen'||self.age>22"];
NSArray *fltPerson2=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson2满足条件的是:%@",fltPerson2);
predicate=[NSPredicatepredicateWithFormat:@"!self.name=='listen'&&self.age<22"];
NSArray *fltPerson3=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson3满足条件的是:%@",fltPerson3);
//访问对象属性时 self.可以省略 但是如果是用对象本身的值(比如NSNumber类的对象)时,要用SELF
//BEGINSWITH (以xxxx开头) ENDsWITH (以xxx结尾) CONTAINS (包含xxx)
//[cd]中的c是不区分大小写 d是不区分重音符号
NSLog(@"\n================BEGINSWITH ENDsWITH CONTAINS================");
predicate=[NSPredicatepredicateWithFormat:@"name BEGINSWITH[cd] 'l'"];
NSArray *fltPerson4=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson4满足条件的是:%@",fltPerson4);
predicate=[NSPredicatepredicateWithFormat:@"name endswith 'o'"];
NSArray *fltPerson5=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson5满足条件的是:%@",fltPerson5);
predicate=[NSPredicatepredicateWithFormat:@"name contains 'e'"];
NSArray *fltPerson6=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson6满足条件的是:%@",fltPerson6);
//IN(指定离散的范围 只和{}中的值匹配) BETWEEN(指定一个闭区间范围{num1,num2}表示num1~num2之间的值)
NSLog(@"\n================取范围================");
predicate=[NSPredicatepredicateWithFormat:@"age IN {22,24}"];
NSArray *fltPerson7=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson7满足条件的是:%@",fltPerson7);
predicate=[NSPredicatepredicateWithFormat:@"age between {22,24}"];
NSArray *fltPerson8=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson8满足条件的是:%@",fltPerson8);
//ALL(所有的都满足) ANY(至少有一个满足)施加的是集合对象
NSLog(@"\n================all和any================");
predicate =[NSPredicatepredicateWithFormat:@"all age>22"];
BOOL result1=[predicateevaluateWithObject:personArray];
if (result1) {
NSLog(@"所有人年龄都大于22");
}
else
{
NSLog(@"有不满二十岁的");
}
predicate =[NSPredicatepredicateWithFormat:@"any age>22"];
BOOL result2=[predicateevaluateWithObject:personArray];
if (result2) {
NSLog(@"至少有一个人大于22");
}
else
{
NSLog(@"所有的人都不满二十二岁");
}
NSLog(@"\n================谓词占位符================");
//%k是key的占位符 只能在谓词中使用
NSString *key=@"name";
NSString *value=@"lgeng";
predicate =[NSPredicatepredicateWithFormat:@"%K==%@",key,value];
NSArray *fltPerson9=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson9满足条件的是:%@",fltPerson9);
//谓词中使用变量($NAME)
NSLog(@"\n================谓词模板================");
//先生成一个谓词模板
NSPredicate *predicateTempLate=[NSPredicatepredicateWithFormat:@"name==$NAME"];
//由谓词模板生成谓词
//1.创建一个字典
NSString *name=@"listen";
NSDictionary *dict=@{@"NAME":name};
//2.生成一个具体的谓词 用字典中的Value 来取代模板的变量
predicate =[predicateTempLate predicateWithSubstitutionVariables:dict];
NSArray *fltPerson10=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson10满足条件的是:%@",fltPerson10);
predicateTempLate=[NSPredicatepredicateWithFormat:@"age between$RANGE"];
NSNumber *beginAge=@20;
NSNumber *endAge=@23;
dict=@{@"RANGE":@[beginAge,endAge]};
predicate=[predicateTempLate predicateWithSubstitutionVariables:dict];
NSArray *fltPerson11=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson11满足条件的是:%@",fltPerson11);
//谓词中使用通配符 like (*匹配多个字符)(?匹配单个字符 但是可以叠加??就是两个字符)
predicate=[NSPredicatepredicateWithFormat:@"name like'l*'"];
NSArray *fltPerson12=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson12满足条件的是:%@",fltPerson12);
predicate=[NSPredicatepredicateWithFormat:@"name like't?'"];
NSArray *fltPerson13=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson13满足条件的是:%@",fltPerson13);
predicate=[NSPredicatepredicateWithFormat:@"name like'?o'"];
NSArray *fltPerson14=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson14满足条件的是:%@",fltPerson14);
predicate=[NSPredicatepredicateWithFormat:@"name like'*n'"];
NSArray *fltPerson15=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson15满足条件的是:%@",fltPerson15);
//正则表达式matches
NSLog(@"\n================谓词正则表达式================");
predicate =[NSPredicatepredicateWithFormat:@"name matches '^l.*n$'"];
NSArray *fltPerson16=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson16满足条件的是:%@",fltPerson16);
predicate =[NSPredicatepredicateWithFormat:@"name matches '^ra.?n$'"];
NSArray *fltPerson17=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson17满足条件的是:%@",fltPerson17);
predicate =[NSPredicatepredicateWithFormat:@"name matches '^..*[1]$'"];
NSArray *fltPerson18=[personArrayfilteredArrayUsingPredicate:predicate];
NSLog(@"fltPerson18满足条件的是:%@",fltPerson18);
predicate =[NSPredicatepredicateWithFormat:@"name matches '^.*$'"];
NSArray *fltPerson19=[personArrayfilteredArrayUsingPredicate: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"
)
- objective-c之谓词
- Objective-C之谓词NSPredicate
- objective-C之 NSPredicate(谓词)
- iOS基础知识:Objective-C 之 谓词
- Objective-c中的谓词
- objective-c 谓词
- Objective-C 谓词
- LHF Objective-C语法(14)谓词
- objective-C学习 谓词NSPredicate 可过滤
- Objective-C中谓词(NSPredicate)的应用
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- objective-c中使用cocoa的NSPredicate,谓词
- objective-c中使用cocoa的NSPredicate,谓词(十四)
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- UVa 11824 - A Minimum Land Price
- 深入浅出设计模式(二):2.简单工厂模式 3.工厂方法模式
- Java提高篇(二八)------TreeSet
- 黑马程序员--面向对象
- 网路图片加载
- objective-c之谓词
- 安卓JNI--小项目试验计时器
- z-index无效
- vretica自动收集统计信息脚本
- 基于Winodws平台的Github使用(初级):
- 12306模拟登录
- 工作环境搭建(10) - CentOS7安装OpenCV(带Python开发环境)
- 自定义万能的progressView
- 打包 ipa (转载)