Predicate,又一次看到了。從c++轉過來的時候就開始關注它是否支援c++的STL和predicate。看到都是支援的。誰讓他們都是C的超集呢。
可以參照:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794
示範下代碼。
NSMutableArray *array =
[NSMutableArray arrayWithObjects:@"Miguel", @"Ben", @"Adam", @"Melissa", nil];
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];
// beginWithB contains { @"Adam" }.
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'e'"];
[array filterUsingPredicate:sPredicate];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@",
[NSArray arrayWithObjects:@"Stig", @"Shaffiq", @"Chris", nil]];
BOOL result = [predicate evaluateWithObject:@"Shaffiq"];
關於正澤運算式可以參照類
NSRegularExpression Class Reference
:http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html