OC之谓词
来源:互联网 发布:入驻淘宝外卖收费标准 编辑:程序博客网 时间:2024/06/05 21:56
//// main.m// 谓词//// Created by New-World on 13-11-6.// Copyright (c) 2013年 Gary. All rights reserved.//#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]; } NSNumber *a=[person age]; int s=[a intValue]; [person setAge:@(i+s)]; [array addObject:person]; [person release]; } //判断是否满足条件 //NSArray *inArray=@[@"tom-8",@"jack-3",@"jack-2"]; //NSPredicate *predicate=[NSPredicate predicateWithFormat:@"age<25"]; //NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name in %@",inArray];//名字是否在数组inArray中 //NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name BEGINSWITH 't'|| name BEGINSWITH 'j'"];//是否以‘t’或者'j'开头 //NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name ENDSWITH '-5'"];//是否以‘-5’结尾 NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name like '*4'"];//是否以‘4’结尾 for (Person *p in array) { BOOL ret=[predicate evaluateWithObject:p]; if (ret) { NSLog(@"%@",p); } } //对数组进行过滤 NSArray *filterArray=[array filteredArrayUsingPredicate:predicate]; NSLog(@"%@",filterArray); } return 0;}