iOS-OC-基礎-NSPredicate常用方法

來源:互聯網
上載者:User

標籤:

NSpredicate 常用方法

    // 謂詞的條件查詢 > 、< 、==、!=    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"age > 20"];    NSArray *arr1 = [stuArray filteredArrayUsingPredicate:predicate1];    NSLog(@"arr1 = %@",arr1);        //使用運算式    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"age > 20 && height > 170"];    NSArray *arr2 = [stuArray filteredArrayUsingPredicate:predicate2];    NSLog(@"arr2 = %@",arr2);        // 字串作為條件要使用單引號‘’,name == ‘lisi‘    NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name == ‘lisi‘"];    NSArray *arr3 = [stuArray filteredArrayUsingPredicate:predicate3];    NSLog(@"arr3 = %@",arr3);        // 使用預留位置    NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"age == %ld",20];    NSArray *arr4 = [stuArray filteredArrayUsingPredicate:predicate4];    NSLog(@"arr4 = %@",arr4);            // IN {‘lisi‘,‘wangwu‘} 在包含的條件內    NSPredicate *predicate5 = [NSPredicate predicateWithFormat:@"height IN {165,180}"];    NSArray *arr5 = [stuArray filteredArrayUsingPredicate:predicate5];    NSLog(@"arr5 = %@",arr5);            //BEGINSWITH 以什麼字串開頭    NSPredicate *predicate6 = [NSPredicate predicateWithFormat:@"name BEGINSWITH[c] ‘R‘"];    NSArray *arr6 = [stuArray filteredArrayUsingPredicate:predicate6];    NSLog(@"arr6 = %@",arr6);            // ENDSWITH 以什麼字串結尾    NSPredicate *predicate7 = [NSPredicate predicateWithFormat:@"name ENDSWITH ‘e‘"];    NSArray *arr7 = [stuArray filteredArrayUsingPredicate:predicate7];    NSLog(@"arr7 = %@",arr7);            // CONTAINS 包含某個字串    NSPredicate *predicate8 = [NSPredicate predicateWithFormat:@"name CONTAINS ‘a‘"];    NSArray *arr8 = [stuArray filteredArrayUsingPredicate:predicate8];    NSLog(@"arr8 = %@",arr8);            /*          LIKE:          1、 LIKE ‘*a*‘ 包含某個字串     2、 LIKE ‘j*‘ 以某個字串開頭     3、 LIKE ‘*e 以某個字串結尾     4、 LIKE ‘?a*‘ 第二個字串是 a 的     5、 LIKE[c] ‘r*‘ [c] 表示忽略大小寫          *: 星號表示任意個字元位,?問號,表示一個字元位,[c] 表示忽略大小寫          */        NSPredicate *predicate9 = [NSPredicate predicateWithFormat:@"name LIKE ‘*a*‘"];    NSArray *arr9 = [stuArray filteredArrayUsingPredicate:predicate9];    NSLog(@"arr9 = %@",arr9);        NSPredicate *predicate10 = [NSPredicate predicateWithFormat:@"name LIKE[c] ‘r*‘"];    NSArray *arr10 = [stuArray filteredArrayUsingPredicate:predicate10];    NSLog(@"arr10 = %@",arr10);        NSPredicate *predicate11 = [NSPredicate predicateWithFormat:@"name LIKE ‘*e‘"];    NSArray *arr11 = [stuArray filteredArrayUsingPredicate:predicate11];    NSLog(@"arr11 = %@",arr11);        NSPredicate *predicate12 = [NSPredicate predicateWithFormat:@"name LIKE ‘?a*‘"];    NSArray *arr12 = [stuArray filteredArrayUsingPredicate:predicate12];    NSLog(@"arr12 = %@",arr12);            // NSMutableArray 使用 filterUsingPredicate,不會返回一個新數組,而是直接覆蓋原來的可變數組    NSPredicate *predicate13 = [NSPredicate predicateWithFormat:@"name LIKE ‘?a*‘"];    [stuArray filterUsingPredicate:predicate13];    NSLog(@"stuArray = %@",stuArray);

 

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_NSPredicate

iOS-OC-基礎-NSPredicate常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.