ios开发学习系列Objective-CiOS学习总结

iOS-基础知识--谓词使用举例

2016-05-14  本文已影响918人  云之君兮鹏

闲敲棋子落灯花


谓词:

谓词,用来描述或判定客体性质、特征或者客体之间关系的词项。

Coco为我们提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。谓词表示计算真值或假值的函数。

OC中的谓词操作是针对于数组类型的,与数据库中的查询操作类似,数据源就是我们要查询的数组,我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用(按条件筛选),使用简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。在iOS中可以用谓词与搜索栏 UISearchController结合使用。


谓词使用简单举例:


Person *per0 = [Person personWithName:@"Note3" age:55];
Person *per1 = [Person personWithName:@"AA" age:20];
Person *per2 = [Person personWithName:@"DD" age:25];
Person *per3 = [Person personWithName:@"GG" age:92];
Person *per4 = [Person personWithName:@"KK" age:38];
Person *per5 = [Person personWithName:@"2324" age:138];
Person *per6 = [Person personWithName:@"詹姆斯" age:81];
Person *per7 = [Person personWithName:@"周琦" age:18];
Person *per8 = [Person personWithName:@"韦德" age:46];
Person *per9 = [Person personWithName:@"HUAWEI3x" age:67];

NSArray *persons = [NSArray arrayWithObjects:per0, per1, per2, per3, per4, per5, per6, per7, per8, per9, nil];

//定义谓词对象,谓词对象中包含了过滤条件(筛选的条件)
//条件1: 年龄大于180的

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 180"];

//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
 for (Person *per in array) 
{
    NSLog(@"条件1 age >180 --> 名字:%@, 年龄:%ld", per.name, per.age);
}
结果截图

//条件2 年龄小于50并且名字为韦德的

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = '韦德' && age < 50"];
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
for (Person *per in array) {
    NSLog(@"条件2 age < 50 && name = 韦德 --> %@, %ld", per.name, per.age);
}
结果截图

//条件3 使用in (包含)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'D','AA', '韦德'} "];
//这句话意思找到在IN后面的这个数组中元素作为名字的人,完全匹配才行

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"满足条件人数 %ld 个", array.count);
for (Person *per in array) {
NSLog(@"条件3结果 %@, %ld", per.name, per.age);
}
条件3结果

//条件4 name以H开头的 (beginswith)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith 'H' "];

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"满足条件的人数 %ld 个", array.count);
for (Person *per in array) {
NSLog(@"条件4结果 %@, %ld", per.name, per.age);
}
条件4结果

//条件5 name以e3结尾的 (endswith)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name endswith 'e3'"];

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"满足条件人数 %ld 个", array.count);
for (Person *per in array) {
NSLog(@"条件5结果 %@, %ld", per.name, per.age);
}
条件5结果

//条件6 name中包含32的(不管在名字中的位置)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains '32'"];

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"满足条件 %ld 个", array.count);
for (Person *per in array) {
NSLog(@"条件6结果 %@, %ld", per.name, per.age);
}
条件6结果

//条件7 name中相应位置有某些字符 ?表示一个字符 *表示0个或多个字符

//predicate = [NSPredicate predicateWithFormat:@"name like '*3?'"];
这个条件意思 : 第二个字符是3后面几个字符都行
//predicate = [NSPredicate predicateWithFormat:@"name like '?a*'"];  
这个条件意思是:倒数第二个字符是a a前面不管几个字符都行
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '*姆*'"];
这个条件意思是: 名字是三个字符  且第二个字符是姆的

NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"%ld", array.count);
for (Person *per in array) {
NSLog(@"条件7结果 %@, %ld", per.name, per.age);
}
条件7结果

������������������

上一篇下一篇

猜你喜欢

热点阅读