标签:
/*
.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic) int pid;
@property (nonatomic,copy) NSString *name;
@property (nonatomic) int age;
@end
*/
/*
.m
#import "Person.h"
@implementation Person
- (NSString *)description
{
return [NSString stringWithFormat:@"pid=%d,name=%@,age=%d",self.pid,self.name,self.age];
}
@end
*/
#import <Foundation/Foundation.h>
#import "Person.h"
//#define a 1
//#define b 1
//#define c 1
//#define d 1
//#define e 1
//#define f 1
#define g 1
int main(int argc, const char * argv[]) {
@autoreleasepool {
//谓词总结
//可以对集合框架进行运算
//对数组等用比较逻辑,SELF,BETWEEN等进行运算
Person *p1 = [[Person alloc]init];
p1.pid = 1;
p1.name = @"tom";
p1.age = 20;
Person *p2 = [[Person alloc]init];
p2.pid = 21;
p2.name = @"kite";
p2.age =20;
Person *p3 = [[Person alloc]init];
p3.pid = 3;
p3.name = @"lg";
p3.age = 31;
#if g
// ?代表一个 ??代表两个 *代表任意多的
NSArray *array2 = @[@"tom1",@"tom12",@"tom123",@"big kite",@"xito m"];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"SELF LIKE ‘tom??‘"];
array2 = [array2 filteredArrayUsingPredicate:pre3];
NSLog(@"%@",array2);
#endif
#if e
NSArray *array2 = @[@"li111",@"li22",@"to m",@"big kite",@"xito m"];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"SELF ENDSWITH %@",@"to m"];
array2 = [array2 filteredArrayUsingPredicate:pre3];
NSLog(@"%@",array2);
#endif
#if f
NSArray *array2 = @[@"li111",@"li22",@"to m",@"big kite",@"xito m"];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",@"l"];
array2 = [array2 filteredArrayUsingPredicate:pre3];
NSLog(@"%@",array2);
#endif
#if d
NSArray *array2 = @[@"li111",@"li22",@"tom",@"big kite",@"xitomuo"];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@",@"tom"];
array2 = [array2 filteredArrayUsingPredicate:pre3];
NSLog(@"%@",array2);
#endif
#ifdef c
NSArray *array2 = @[@"li111",@"li22",@"tom",@"big kite",@"xiaoruo"];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"self == %@",@"tom"];
array2 = [array2 filteredArrayUsingPredicate:pre3];
NSLog(@"%@",array2);
#endif
#ifdef b
NSArray *array1 = @[[NSNumber numberWithInt:20],[NSNumber numberWithInt:30]];
NSPredicate *pr = [NSPredicate predicateWithFormat:@"age BETWEEN %@",array1];
NSArray *array = @[p1,p2,p3];
array = [array filteredArrayUsingPredicate:pr];
NSLog(@"%@",array);
#endif
#ifdef a
//1.谓词 NSPredicate
//2.作用对集合进行运算
NSPredicate *pre = [NSPredicate predicateWithFormat:@"%K=%d && %K=%@",@"age",20,@"name",@"tom"];
//NSPredicate *pre = [NSPredicate predicateWithFormat:@"%K=%d && %K=%@",@"age",20,@"name",@"tom"];
NSArray *array = @[p1,p2,p3];
array = [array filteredArrayUsingPredicate:pre];
NSLog(@"%@",array);
for(Person *per in array)
{
BOOL r = [pre evaluateWithObject:per];
if(r)
{
NSLog(@"%@,%d",per.name,per.age);
}
}
#endif
}
return 0;
}
//.............................................................................................
/*
(2)也可以利用谓词先过滤一下这个数组,然后再输出:
NSArray *arr2=[mutarr1 filteredArrayUsingPredicate:predic1];NSLog(@"%@",arr2);
当然,这样做之前,需要先定义对象的打印方法,在Person.m文件中:
#import "Person.h"@implementation Person-(NSString *)description{NSString *s=[NSString stringWithFormat:@"%@,%@",_name,_age];return s;}@end
(3)也可以使用占位符,以下的几条主要是针对条件里面的书写的
NSPredicate *predic1=[NSPredicate predicateWithFormat:@"age<%d",25];
(4)也可以使用and和or(&&和||)都可以
NSPredicate *predic1=[NSPredicate predicateWithFormat:@"age<%d && age>21",25];
(5)用in。in后面也可以跟一个数组,也就是要再数组里面。
NSPredicate *predic1=[NSPredicate predicateWithFormat:@"name in {‘jack_0‘,‘jack_1‘}"];
(6)还可以用BEGINSWITH和ENDSWITH来匹配,大小写敏感
NSPredicate *predic1=[NSPredicate predicateWithFormat:@"name BEGINSWITH‘j‘ || ENDSWITH ‘1‘"];
(7)CONTAINS和LIKE包含查询
NSPredicate *predic1=[NSPredicate predicateWithFormat:@"name CONTAINS ‘a‘ "];NSPredicate *predic1=[NSPredicate predicateWithFormat:@"name LIKE ‘*a*‘ "];
*/
标签:
原文地址:http://www.cnblogs.com/wanghengheng/p/4787853.html