让我们来了解一下OC中的一种谓词,即NSPredicate,其中NSPredicate定义了一个逻辑查询条件,可以用来过滤数组中的元素,创建NSPredicate对象可以通过+predicateWithFormat:arguments:方法实现,在oc中为:
+(NSPredicate *)predicateWithFormat:(NSString *)format arguments:(va_list)argList
谓词操作是针对于数组类型的,他就好比数据库中的查询操作,我们根据给定的数据源按条件进行查询,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。下面看看博主自己写的用来测试的代码:
#import <Foundation/Foundation.h>
@interface Student : NSObject
//Student类的属性
//名字
@property NSString *name;
//年龄
@property int age;
//成绩
@property int score;
//用于初始化Student对象的类方法
+(Student *)initWithStudent:(NSString *)name andAge:(int)age andScore:(int)score;
@end
@implementation Student
+(Student *)initWithStudent:(NSString *)name andAge:(int)age andScore:(int)score
{
Student *student = [[Student alloc] init];
student.name = name;
student.age = age;
student.score = score;
return student;
}
//重写description方法,用于输出Student对象的信息
-(NSString *)description
{
NSString *s = [[NSString alloc] initWithFormat:@"student:name = %@,age = %d,score = %d",_name,_age,_score];
return s;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建含有10个学生信息的数组(一个数组元素包含学生名字、年龄、成绩)
NSArray *stu = [[NSArray alloc] initWithObjects:
[Student initWithStudent:@"2" andAge:21 andScore:98],
[Student initWithStudent:@"1" andAge:21 andScore:90],
[Student initWithStudent:@"3" andAge:23 andScore:99],
[Student initWithStudent:@"5" andAge:22 andScore:89],
[Student initWithStudent:@"4" andAge:20 andScore:85],
[Student initWithStudent:@"6" andAge:21 andScore:80],
[Student initWithStudent:@"7" andAge:19 andScore:92],
[Student initWithStudent:@"8" andAge:22 andScore:81],
[Student initWithStudent:@"10" andAge:23 andScore:96],
[Student initWithStudent:@"9" andAge:20 andScore:86], nil];
//查询成绩为90分或者90分以上的学生信息
//创建谓词对象,包含过滤的条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"score >= %d",90];
//使用谓词条件过滤数组中的元素,过滤后,并且返回查询的结果
NSArray *array = [stu filteredArrayUsingPredicate:predicate];
//输出查询的结果
NSLog(@"array = %@",array);
//查询年龄大于20岁的学生信息
predicate = [NSPredicate predicateWithFormat:@"age > %d",20];
array = [stu filteredArrayUsingPredicate:predicate];
NSLog(@"array = %@",array);
//查询年龄大于20岁且成绩为90分或者90分以上的学生信息
predicate = [NSPredicate predicateWithFormat:@"age > %d && score >= %d",20,90];
array = [stu filteredArrayUsingPredicate:predicate];
NSLog(@"array = %@",array);
}
return 0;
}
运行结果截图:
再来看看博主写的另一个测试代码,包含了查询数组中含有某个特定元素,或者查询以某个字符开头的数组元素,如下:
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *name;
@property int age;
@property NSString *sex;
@end
@implementation Person
+(Person *)initWithPerson:(NSString *)name andAge:(int)age andSex:(NSString *)sex
{
Person *person = [[Person alloc] init];
person.name = name;
person.age = age;
person.sex = sex;
return person;
}
-(NSString *)description
{
NSString *str = [[NSString alloc] initWithFormat:@"Person:name = %@,age = %d,sex = %@",_name,_age,_sex];
return str;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *person = [[NSArray alloc] initWithObjects:
[Person initWithPerson:@"xiaobo" andAge:22 andSex:@"男"],
[Person initWithPerson:@"xiaojin" andAge:23 andSex:@"男"],
[Person initWithPerson:@"xiaohong" andAge:21 andSex:@"女"],
[Person initWithPerson:@"xiaolan" andAge:19 andSex:@"女"],
[Person initWithPerson:@"xiaoming" andAge:20 andSex:@"男"],
[Person initWithPerson:@"Bob" andAge:24 andSex:@"男"],
[Person initWithPerson:@"qianqian" andAge:20 andSex:@"女"],
[Person initWithPerson:@"minmin" andAge:18 andSex:@"女"],
[Person initWithPerson:@"xiaozhou" andAge:22 andSex:@"男"],
[Person initWithPerson:@"xinlei" andAge:23 andSex:@"女"], nil];
//查询age > 20的person信息
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > %d",20];
NSArray *array1 = [person filteredArrayUsingPredicate:predicate];
NSLog(@"array1 = %@",array1);
NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",@"ten", nil];
//查询含有字符‘t’的数组元素
predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 't'"];
NSArray *array2 = [array filteredArrayUsingPredicate:predicate];
NSLog(@"array2 = %@",array2);
//查询以字符‘t’开头的数组元素
predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 't'"];
NSArray *array3 = [array filteredArrayUsingPredicate:predicate];
NSLog(@"array3 = %@",array3);
}
return 0;
}
运行结果截图:
以上是博主学习oc中用到谓词(NSPredicate)来查询符合条件的数组也即按条件过滤数组的总结,谢谢读者的浏览。