Objective-C(十八、謂語使用及執行個體說明)——iOS開發基礎

來源:互聯網
上載者:User

標籤:ios開發基礎   oc   謂詞   模糊查詢   in包含   

結合之前的學習筆記以及參考《Objective-C編程全解(第三版)》,對Objective-C知識點進行梳理總結。知識點一直在變,只是作為參考,以蘋果官方文檔為準~

十八、謂語的使用及執行個體說明

首先先介紹基本常用的謂詞:
(1)邏輯運算子 && AND || OR 都可以用
(2)IN包含
(3)模糊查詢
a、以……開頭 BEGINSWITH
b、以……結尾 ENDSWITH
c、包含….字元 CONTAINS
(4)用like進行模糊查詢
萬用字元:*表示任意個字元 ?表示單個字元

like *a  以a結尾like a*  以a開頭like *a* 包含a字元like ?a* 第二個字元為a的字串

執行個體說明:

建立Book類,Book.h

@interface Book : NSObject{    NSInteger _price;    NSString* _bookName;}- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName;@end

Book.h

#import "Book.h"@implementation Book- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName {    if (self = [super init]) {        _price = price;        _bookName = bookName;    }    return self;}- (NSString *)description {    return [NSString stringWithFormat:@"Book price:%li,named %@",_price,_bookName];}@end

main.m

int main(int argc, const char * argv[]) {    @autoreleasepool {        Book* book1 = [[Book alloc] initWithPrice:20 andBookName:@"C Programming"];        Book* book2 = [[Book alloc] initWithPrice:32 andBookName:@"C++ Programming"];        Book* book3 = [[Book alloc] initWithPrice:18 andBookName:@"Java Programming"];        Book* book4 = [[Book alloc] initWithPrice:45 andBookName:@"OC guiding"];        Book* book5 = [[Book alloc] initWithPrice:28 andBookName:@"iOS guiding"];        NSArray* books = [NSArray arrayWithObjects:book1,book2,book3,book4,book5, nil];        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"price > %i",30];        NSArray *filterArray = [books filteredArrayUsingPredicate:predicate];        NSLog(@"%@",filterArray);//      邏輯運算子 和 IN        predicate = [NSPredicate predicateWithFormat:@"bookName IN {‘C Programming‘,‘C++ Programming‘} AND price > 30"];        filterArray = [books filteredArrayUsingPredicate:predicate];        NSLog(@"%@",filterArray);//      模糊查詢 和 用萬用字元查詢        predicate = [NSPredicate predicateWithFormat:@"bookName CONTAINS ‘guiding‘ || bookName like ‘*Program*‘ "]; //包含guiding或者包含Program        filterArray = [books filteredArrayUsingPredicate:predicate];        NSLog(@"%@",filterArray);    }    return 0;}

output:

2015-07-09 20:17:24.403 exercise_謂語[632:9877] (    "Book price:32,named C++ Programming",    "Book price:45,named OC guiding")2015-07-09 20:17:24.404 exercise_謂語[632:9877] (    "Book price:32,named C++ Programming")2015-07-09 20:17:24.407 exercise_謂語[632:9877] (    "Book price:20,named C Programming",    "Book price:32,named C++ Programming",    "Book price:18,named Java Programming",    "Book price:45,named OC guiding",    "Book price:28,named iOS guiding")

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Objective-C(十八、謂語使用及執行個體說明)——iOS開發基礎

聯繫我們

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