IOS 谓词

// 谓词

Person *person = [[Person alloc]init];

person.name = @"zhang san";

person.age = 20;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like 'zhang1*'"];

BOOL resultFlag = [predicate evaluateWithObject:person];

NSLog(@"resultFlag = %i", resultFlag);

NSString *testStr = @"我是测试的字符串";

NSPredicate *strPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS '测试'"];

BOOL resultFlag1 = [strPredicate evaluateWithObject:testStr];

NSLog(@"resultFlag1 = %i", resultFlag1);

// 过滤简单集合

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"高级中学1班",@"初级中学1班",@"初级中学2班",@"小学六年级",@"高级中学2班", nil];

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF like '高级中学*'"];

[array filterUsingPredicate:predicate1];

NSLog(@"array = %@", array);

// 过滤对象集合

Person *person1 = [[Person alloc]init];

person1.name = @"student1";

person1.age = 20;

Person *person2 = [[Person alloc]init];

person2.name = @"student2";

person2.age = 21;

Person *person3 = [[Person alloc]init];

person3.name = @"student3";

person3.age = 22;

Person *person4 = [[Person alloc]init];

person4.name = @"student4";

person4.age = 23;

NSMutableArray *studentArray = [[NSMutableArray alloc]initWithObjects:person,person1,person2,person3,person4, nil];

NSLog(@"studentArray = %@", studentArray);

//    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name CONTAINS 'student'"];

//    [studentArray filterUsingPredicate:predicate2];

//    NSLog(@"studentArray = %@", studentArray);

//

//    NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"age > 21"];

//    [studentArray filterUsingPredicate:predicate3];

//    NSLog(@"studentArray = %@", studentArray);

// 谓词中使用占位符  %K - 属性名占位符  %@ - 属性值占位符

NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"name", @"student"];

[studentArray filterUsingPredicate:predicate4];

NSLog(@"studentArray = %@", studentArray);

上一篇:js对象数组(JSON) 根据某个共同字段 分组


下一篇:selenium webdriver 如何添加cookie