Micro-word (filter), micro-word Filtering
I personally think that microwords are a good thing.
1. Custom object
@interface Person : NSObject@property(nonatomic , copy)NSString * name;@property(nonatomic , assign)int age;@property (nonatomic , retain)NSArray * familys;@end
2. simple and practical (data comparison)
-(Void) testObject1 {Person * person = [[Person alloc] init]; person. name = @ "gulong"; person. age = 24; // compare NSPredicate * pred = [NSPredicate predicateWithFormat: @ "age> 25"]; BOOL match = [pred evaluateWithObject: person]; NSLog (@ "% s", (match )? "YES": "NO ");
// Printf: NO}
3. Wildcards and Regular Expressions
-(Void) testObject2 {/* BEGINSWITH: Check whether a string starts with another string. ENDSWITH: Check whether a string ends with another string. CONTAINS: checks whether a string is inside another string. [C] case-insensitive [d] No pronunciation symbol, that is, no accent symbol [cd] is case-insensitive and does not distinguish pronunciation symbols. */Person * person = [[Person alloc] init]; person. name = @ "gulong"; person. age = 24;/* wildcard */NSPredicate * predicate1 = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '* er *'"]; // * indicates that the wildcard Like also accepts [cd]. predicate1 = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '??? Er * '"]; //? It only matches one character and can accept [cd]. // * indicates multiple placeholders/* regular match */NSString * regex = @ "[A-Za-z] +"; NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "self matches % @", regex]; if ([predicate evaluateWithObject: @ "huang"]) {NSLog (@ "Whether the string is a pure Letter ");} // whether the printf string is a pure letter
}
4. Filter Array 1 (each object in it is a string)
-(Void) testArray1 {NSArray * array = [[NSArray alloc] initWithObjects: @ "beijing", @ "shanghai", @ "guangzou", @ "wuhan", nil]; NSString * string = @ "ang"; // obtain all strings with ang and form an array NSPredicate * pred = [NSPredicate predicateWithFormat: @ "self contains % @", string]; NSLog (@ "% @", [array filteredArrayUsingPredicate: pred]); // printf: shanghai, guangzou}
5. Filter Arrays 2 (use the custom person class)
-(Void) testArray2 {Person * person1 = [[Person alloc] init]; person1.name = @ "long"; person1.age = 23; person * person2 = [[Person alloc] init]; person2.name = @ "gu"; person2.age = 23; Person * person3 = [[Person alloc] init]; person3.name = @ "long"; person3.age = 24; NSArray * persons = @ [person1, person2, person3]; // you can directly use the attributes in the object, an object name of an array cannot be used (in fact, NSPredicate * pred = [NSPredicate predicateWithFormat: @ "name = 'long'"]; // equal // (age> 23) AND (age <26) AND both must satisfy, OR, you can use either of the two methods: // age between {} from 23 to 26 // @ "name IN {'herbie ', 'snugs', 'badger ', 'flap'} "; // include IN NSArray * array = [persons filteredArrayUsingPredicate: pred]; NSLog (@" array % @ ", [array description]);}
6. array filtering 3 (there is data in the object)
@property (nonatomic , retain)NSArray * familys; //person.h
-(Void) testArray3 {Person * person1 = [[Person alloc] init]; person1.name = @ "huang"; person1.age = 23; person1.familys = @ [@ "zhuge ", @ "zhangfei", @ "liubei"]; Person * person2 = [[Person alloc] init]; person2.name = @ "gu"; person2.age = 23; person2.familys = @ [@ "suiquan", @ "huanggai", @ "lusun"]; Person * person3 = [[Person alloc] init]; person3.name = @ "huang "; person3.age = 24; person3.familys = @ [@ "caochao", @ "xiahou", @ "caozhi"]; NSArray * persons = @ [person1, person2, person3]; /** the array must contain the entire string, not a part (such as the huang of huanggai; in this case, there is no data) */NSPredicate * pred = [NSPredicate predicateWithFormat: @ "familys CONTAINS 'huanggai'"]; NSArray * array = [persons filteredArrayUsingPredicate: pred]; NSLog (@ "array % @", [array description]); // print person2}