NSPredicate 的用法舉例,nspredicate舉例
Cocoa 提供了一個名為NSPredicate的類,用於指定過濾器的條件,用NSPredicate描述查詢方式,原理類似於在資料庫中進行查詢。可以在資料庫風格的API中使用NSPredicate類,常見的用於Core Data和Spotlight。 本文講解過程中用到了部分類比如garage(車庫的類),car(車)等僅供舉例,未列出建立代碼,體會predicate的用法才是關鍵。
建立Predicate.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'Herbie'"];
計算Predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"engine.horsepowe > 150"]; NSArray *cars = [garage cars]; for (Car *car in [garage cars]) { if ([predicate evaluateWithObject:car]) { NSLog(@"%@",car.name); } }
遍曆數組取出合格對象。
數組過濾器
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"engine.horsepowe > 150"]; NSArray *results; results = [cars filteredArrayUsingPredicate:predicate];
假如你有一個可變數組,而且需要剔除不屬於該數組的所有項目。使用以下方法:
NSMutableArray *carsCopy = [cars mutableCopy]; [carsCopy filterUsingPredicate:predicate];
構造靈活Predicate
有兩種方式,
第一種 格式說明符
NSPredicate *predicate1, *predicate2, *predicate3; predicate1 = [NSPredicate predicateWithFormat:@"engine.horsepowe > %d",50]; predicate2 = [NSPredicate predicateWithFormat:@"name == %@",@"Herbie"]; predicate3 = [NSPredicate predicateWithFormat:@"%k == %@", @"name", @"Herbie"];
第二種 將變數名放入字串中,類似於環境變數
此predicate等價於第一種裡的的predicate3和predicate2
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"]; NSDictionary *varDict; varDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Herbie",@"NAME", nil]; NSPredicate *predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
而此predicate等價於第一種裡的predicate1
predicateTemplate = [NSPredicate predicateWithFormat:@"engine.horsepower > $POWER"]; varDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:150], @"POWER", nil]; predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
predicate的運算子
支援C語言的常用運算子 == = > < >= <= != <> && || ! AND OR NOT等
predicate = [NSPredicate predicateWithFormat:@"(engine.horsepower > 50) AND (engine.horsepower < 200)"]; results = [cars filteredArrayUsingPredicate:predicate];
數組運算子
predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN{50,200}"];
等價於
NSArray *betweens = [NSArray arrayWithObjects:[NSNumber numberWithInt:50],[NSNumber numberWithInt:200],nil]; predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BEWEEN %@",betweens];
SELF 關鍵詞響應predicate計算對象
NSArray *names1 = [NSArray arrayWithObjects:@"Herbie",@"Badger",@"Judge",@"Elvis", nil]; NSArray *names2 = [NSArray arrayWithObjects:@"Judge",@"Paper Car",@"Badger",@"Phoenix", nil]; predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", names1]; results = [names2 filteredArrayUsingPredicate:predicate]; NSLog(@"%@", results);
答案是:
(
Judge,
Badger
)
字串運算子
BEGINSWITH 檢查某個字串是否以另一個字串開頭
ENDSWITH 檢查某個字串是否以另一個字串結尾
CONTAINS 檢查某個字串是否在另一個字串內部
比如使用 "name BEGINSWITH 'Bad' " 匹配Badger匹配是區分大小寫,如果匹配BADGer就匹配不得了
要忽視大小寫及重音符等規則,可以前面加上[cd]" name BEGINSWITH [cd] 'HERB' " 匹配 Herbie 即可匹配成功 ,通常加上[cd]比較好
LIKE運算子
" name LIKE ' *er* ' "將會與任何含有er的名稱匹配,等價於CONTAINS" name LIKE ' ???er* ' "將會與Paper Car 匹配 但不會與Badger匹配,er前面限定了三個字元如果你熱衷於Regex,可以使用MATCHES 運算子,賦給它一個Regex。
轉載請註明原著:http://blog.csdn.net/marvindev