NSPredicate 的用法舉例,nspredicate舉例

來源:互聯網
上載者:User

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







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.