#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSMutableArray *array = [NSMutableArray array];
for (int i=0; i<10; i++)
{
Person *person = [[Person alloc]init];
if (i<5)
{
person.name = [NSString stringWithFormat:@"jack-%d",i];
}
else
{
person.name = [NSString stringWithFormat:@"tom-%d",i];
}
person.age = @(20+i);
[array addObject:person];
[person release];
}
//判断是否满足条件
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<25"];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",23]; //格式占位符
//逻辑运算符 不区分大小写 and && or ||
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<22 or age>26"];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<26 && age>24"];
//关键字 注意字符串加‘’
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in {'jack-2','tom-8','sss'}"]; //IN
// NSArray *inArray = @[@"jack-2",@"tom-8",@"sss"];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in %@",inArray];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'j'"]; //开头
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH '5'"]; //结尾
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'o'"]; //包含
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '*a*'"]; //模糊查询
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '??m*'"]; //*?通配符
for (Person *p in array) {
BOOL ret = [predicate evaluateWithObject:p];
if (ret)
{
NSLog(@"%@",p);
}
}
//对数组过滤
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filterArray);
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic,retain)NSNumber *age;
@end
#import "Person.h"
@implementation Person
-(NSString *)description{
NSString *s = [NSString stringWithFormat:@"Name = %@,Age = %@",_name,_age];
return s;
}
@end