#import <Foundation/Foundation.h> @interface person:nsobject<nscopying> @property (nonatomic,copy) NSString *name; @property (nonatomic,retain) NSNumber *age;-(void) Setnewname: (NSString *) name; @end
#import "Person.h" @implementation person:nsobject-(ID) Copywithzone: (Nszone *) zone{person *person=[[[self class ] [Allocwithzone:zone] init]; Person.name=_name; Person.age=_age; return person;} -(void) Setnewname: (NSString *) name{ self.name=name;} -(NSString *) description{ nsstring *description=[nsstring stringwithformat:@ "Age:%@; Name:%@", _age,_name]; return description;} @end
predicate usage nspredicate nsmutablearray *array=[nsmutablearray array]; for (int i=0, i<10; i++) {person *person=[[person alloc]init]; [Person setage:@ (20+i)]; Person.name=[nsstring stringwithformat:@ "jage-%d", I]; [Array Addobject:person]; }//large, small, etc, etc arithmetic filtering nspredicate *predicate=[nspredicate predicatewithformat:@ "age<%d", 25]; Nspredicate *predicate2=[nspredicate predicatewithformat:@ "age<23", 23]; Nspredicate *predicate3=[nspredicate predicatewithformat:@ "age<27 and age>25"]; Nspredicate *predicate4=[nspredicate predicatewithformat:@ "age<27 && age>25"]; Nspredicate *predicate5=[nspredicate predicatewithformat:@ "age<23 | | age>26"]; Nspredicate *predicate6=[nspredicate predicatewithformat:@ "name= ' jage-3 '"]; Inside a certain nspredicate *predicate7=[nspredicate predicatewithformat:@ "name in {' Jage-1 ', ' jage-5 '}"]; Nsarray *[email protected][@ "jage-1", @ "jage-4", @ "jage-3"]; Nspredicate *predicate8=[nspredicate predicatewithformat:@ "name in%@", InArray]; What has started, note plus single quotes nspredicate *predicate9=[nspredicate predicatewithformat:@ "name Beginswith ' Jage '"]; What has ended, note the addition of single quotes Nspredicate *predicate10=[nspredicate predicatewithformat:@ "Name ENDSWITH '-9 '"]; Included, note plus single quotes nspredicate *predicate11=[nspredicate predicatewithformat:@ "Name CONTAINS '-3 '"]; Like, note the extra single quote nspredicate *predicate12=[nspredicate predicatewithformat:@ "name like ' jage-?"]; Nspredicate *predicate13=[nspredicate predicatewithformat:@ "name like ' *-2 '"]; For (person *person in array) {if ([Predicate6 Evaluatewithobject:person]) {//Object-by-case NSLog (@ "%@", person); }}//log Group filter Nsarray *filterarray=[array FILTEREDARRAYUSINGPREDICATE:PREDICATE13]; NSLog (@ "%@", Filterarray);