概述Cocoa用NSPredicate描述查询的方式,原理类似于在数据库中进行查询 计算谓词: //基本的查询 NSPredicate *predicate; predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"]; BOOL match = [predicate evaluateWithObject: car];
Cocoa用nspredicate描述查询的方式,原理类似于在数据库中进行查询 计算谓词: //基本的查询 nspredicate *predicate; predicate = [nspredicate predicateWithFormat: @"name == 'HerbIE'"]; BOol match = [predicate evaluateWithObject: car]; NSLog (@"%s",(match) ? "YES" : "NO"); //在整个cars里面循环比较 predicate = [nspredicate predicateWithFormat: @"engine.horsepower > 150"]; NSArray *cars = [garage cars]; for (Car *car in [garage cars]) { if ([predicate evaluateWithObject: car]) { NSLog (@"%@",car.name); } } //输出完整的信息 predicate = [nspredicate predicateWithFormat: @"engine.horsepower > 150"]; NSArray *results; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); //含有变量的谓词 nspredicate *predicateTemplate = [nspredicate predicateWithFormat:@"name == $name"]; NSDictionary *varDict; varDict = [NSDictionary dictionaryWithObjectsAndKeys: @"HerbIE",@"name",nil]; predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; NSLog(@"SnorGLE: %@",predicate); match = [predicate evaluateWithObject: car]; NSLog (@"%s",(match) ? "YES" : "NO"); //注意不能使用$VARIABLE作为路径名,因为它值代表值 //谓词字符窜还支持c语言中一些常用的运算符 predicate = [nspredicate predicateWithFormat: @"(engine.horsepower > 50) AND (engine.horsepower < 200)"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"oop %@",results); predicate = [nspredicate predicateWithFormat: @"name < 'Newton'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",[results valueForKey: @"name"]); //强大的数组运算符 predicate = [nspredicate predicateWithFormat: @"engine.horsepower BETWEEN { 50,200 }"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); NSArray *betweens = [NSArray arrayWithObjects: [NSNumber numberWithInt: 50],[NSNumber numberWithInt: 200],nil]; predicate = [nspredicate predicateWithFormat: @"engine.horsepower BETWEEN %@",betweens]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); predicateTemplate = [nspredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"]; varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens,@"POWERS",nil]; predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); //IN运算符 predicate = [nspredicate predicateWithFormat: @"name IN { 'HerbIE','Snugs','Badger','Flap' }"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",[results valueForKey: @"name"]); predicate = [nspredicate predicateWithFormat: @"SELF.name IN { 'HerbIE',[results valueForKey: @"name"]); names = [cars valueForKey: @"name"]; predicate = [nspredicate predicateWithFormat: @"SELF IN { 'HerbIE','Flap' }"]; results = [names filteredArrayUsingPredicate: predicate];//这里限制了SELF的范围 NSLog (@"%@",results); //BEGINSWITH,ENDSWITH,CONTAINS //附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分 predicate = [nspredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); predicate = [nspredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); predicate = [nspredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); //liKE运算符(通配符) predicate = [nspredicate predicateWithFormat: @"name liKE[cd] '*er*'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); predicate = [nspredicate predicateWithFormat: @"name liKE[cd] '???er*'"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@",results); 总结
以上是内存溢出为你收集整理的正则表达式 NSPredicate 的完全用法全部内容,希望文章能够帮你解决正则表达式 NSPredicate 的完全用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)