關(guān)于iOS中的謂詞NSPredicate

一、引言

在現(xiàn)代漢語的解釋中,謂詞是用來描述或判斷客體性質(zhì)、特征或者客體之間關(guān)系的詞項。通俗的說,它是描述事物屬性的。在iOS開發(fā)Cocoa框架中,有提供NSPredicate類,這個類通常也被成為謂詞類,其主要的作用是在Cocoa中幫助查詢和檢索,但是需要注意,實質(zhì)上謂詞并不是提供查詢和檢索的支持,它是一種描述查詢檢索條件的方式,就像更加標準通用的正則表達式一樣。

NSPredicate提供的謂詞可以分為兩類:比較謂詞和復(fù)合謂詞。

  • 比較謂詞:比較謂詞通過使用比較運算符來描述所符合條件的屬性狀態(tài)。
  • 復(fù)合謂詞:復(fù)合謂詞用來組合多個比較謂詞的結(jié)果,取交集,并集或補集。

對于比較謂詞,可以描述精準的比較也可以通過范圍或者包含等進行模糊比較。需要注意,任何Cocoa類對象都可以支持謂詞,但是此類需要實現(xiàn)鍵值編碼(key-value-coding)協(xié)議。

二、NSPredicate類的應(yīng)用解析

NSPredicate提供創(chuàng)建謂詞對象和解析謂詞對象的方法,它也是Cocoa中有關(guān)謂詞的類中的基類。我們在日常開發(fā)中,NSPredicate類的應(yīng)用頻率也最高。

創(chuàng)建謂詞對象有3種方式,分別是通過格式化字符串創(chuàng)建謂詞,直接通過代碼創(chuàng)建謂詞,通過模板創(chuàng)建謂詞。NSPredicate提供了如下函數(shù)來進行初始化:

//通過格式化字符串來進行謂詞對象的初始化
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(nullable NSArray *)arguments;
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList;

使用格式化字符串進行謂詞的初始化十分靈活,但是需要注意,其謂詞字符串的語法和正則表達式并不一樣,后面會有具體的介紹,下面是一個謂詞檢索示例:

//檢索屬性length為5的對象
    NSPredicate *  predicate = [NSPredicate predicateWithFormat:@"length = 5"];
    //對于這個數(shù)組中的字符串,即是檢索字符串長度為5的元素
    NSArray * test = @[@"sda",@"321",@"sf12",@"dsdwq1",@"swfas"];
    NSArray * result = [test filteredArrayUsingPredicate:predicate];
    //將打印@[@"swfas"]
    NSLog(@"%@",result);

其實,你也可以像使用NSLog函數(shù)一樣來進行格式化字符串的構(gòu)造,可以使用%@,%d等等格式化字符來在運行時替換為變量的實際值。同時也需要注意,這種格式化字符串創(chuàng)建的謂詞語句并不會進行語法檢查,錯誤的語法會產(chǎn)生運行時錯誤,要格外小心。有一個小細節(jié)需要注意,在進行格式化時,如果使用的是變量則不需要添加引號,解析器會幫助你添加,如果使用到常量,則要用轉(zhuǎn)義字符進行轉(zhuǎn)義,例如:

NSPredicate *  predicate = [NSPredicate predicateWithFormat:@"name = %@ && age = \"25\"",name];

對于屬性名,如果也需要進行格式化,需要注意不能使用%@符號,這個符號在解析時會被解析器自動添加上引號,可以使用%K,示例如下:

    NSString * key = @"length";
    NSPredicate *  predicate = [NSPredicate predicateWithFormat:@"%K = 5",key];
    NSArray * test = @[@"sda",@"321",@"sf12",@"dsdwq1",@"swfas"];
    NSArray * result = [test filteredArrayUsingPredicate:predicate];
    //將打印@[@"swfas"]
    NSLog(@"%@",result);

通過模板來創(chuàng)建謂詞對象也是一種十分常用的方式,和格式化字符串不同的是,謂詞模板中只有鍵名,沒有鍵值,鍵值需要在字典中進行提供,例如:

NSPredicate *  predicate = [NSPredicate predicateWithFormat:@"length = $LENGTH"];
    predicate = [predicate predicateWithSubstitutionVariables:@{@"LENGTH":@5}];
    NSArray * test = @[@"sda",@"321",@"sf12",@"dsdwq1",@"swfas"];
    NSArray * result = [test filteredArrayUsingPredicate:predicate];
    //將打印@[@"swfas"]
    NSLog(@"%@",result);

NSPredicate中其他屬性與方法解析如下:

//創(chuàng)建一個總是驗證通過(YES)或不通過(NO)的謂詞對象
/*
如果創(chuàng)建的是驗證通過的,則任何檢索都會成功進行返回,否則任何檢索都會失敗不返回任何對象
*/
+ (NSPredicate *)predicateWithValue:(BOOL)value;
//自定義實現(xiàn)檢索函數(shù)
/*
例如前面的示例也可以這樣寫
NSPredicate * predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([evaluatedObject length]==5) {
            return YES;
        }
        return NO;
    }];
*/
+ (NSPredicate*)predicateWithBlock:(BOOL (^)(id _Nullable evaluatedObject, NSDictionary<NSString *, id> * _Nullable bindings))block;
//格式化字符串屬性
@property (readonly, copy) NSString *predicateFormat;  
//當使用謂詞模板來進行對象創(chuàng)建時,這個函數(shù)用來設(shè)置謂詞模板中變量替換
- (instancetype)predicateWithSubstitutionVariables:(NSDictionary<NSString *, id> *)variables;
//檢查一個Object對象是否可以通過驗證
- (BOOL)evaluateWithObject:(nullable id)object; 
//用謂詞模板進行對象的驗證
- (BOOL)evaluateWithObject:(nullable id)object substitutionVariables:(nullable NSDictionary<NSString *, id> *)bindings;

三、通過代碼來創(chuàng)建謂詞對象

前面我們說有3種創(chuàng)建謂詞對象的方式,有兩種我們已經(jīng)有介紹,通過代碼直接創(chuàng)建謂詞對象是最復(fù)雜的一種。通過代碼來創(chuàng)建謂詞對象十分類似通過代碼來創(chuàng)建Autolayout約束。通過前面我們的介紹,謂詞實際是用表達式來驗證對象,用代碼來創(chuàng)建謂詞實際就是用代碼來創(chuàng)建表達式。

1.先來看NSComparisonPredicate類

這個類是NSPredicate的子類,其用來創(chuàng)建比較類型的謂詞。例如使用下面的代碼來改寫上面的例子:

//創(chuàng)建左側(cè)表達式對象 對應(yīng)為鍵
    NSExpression * left = [NSExpression expressionForKeyPath:@"length"];
    //創(chuàng)建右側(cè)表達式對象 對應(yīng)為值
    NSExpression * right = [NSExpression expressionForConstantValue:[NSNumber numberWithInt:5]];
    //創(chuàng)建比較謂詞對象 這里設(shè)置為嚴格等于
    NSComparisonPredicate * pre = [NSComparisonPredicate predicateWithLeftExpression:left rightExpression:right modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:NSCaseInsensitivePredicateOption];
    NSArray * test = @[@"sda",@"321",@"sf12",@"dsdwq1",@"swfas"];
    NSArray * result = [test filteredArrayUsingPredicate:pre];
    //將打印@[@"swfas"]
    NSLog(@"%@",result);

NSComparisonPredicateModifier用來進行條件的修飾設(shè)置,枚舉如下:

typedef NS_ENUM(NSUInteger, NSComparisonPredicateModifier) {
    NSDirectPredicateModifier = 0, //直接進行比較操作
    NSAllPredicateModifier,  //用于數(shù)組或集合 只有當內(nèi)部所有元素都通過驗證時 集合才算通過
    NSAnyPredicateModifier  //同于數(shù)組或集合 當內(nèi)部有一個元素滿足時 集合算通過驗證
};

關(guān)于NSAllPredicateModifier和NSAnyPredicateModifier,這兩個枚舉專門用于數(shù)組或集合類型對象的驗證,ALL會驗證其中所有元素,全部通過后數(shù)組或集合才算驗證通過,ANY則只要有一個元素驗證通過,數(shù)組或集合就算驗證通過,例如:

NSPredicate *  pre = [NSPredicate predicateWithFormat:@"ALL length = 5"];
 NSArray * test = @[@[@"aaa",@"aa"],@[@"bbbb",@"bbbbb"],@[@"ccccc",@"ccccc"]];
    NSArray * result = [test filteredArrayUsingPredicate:pre];
    //將打印@[@[@"ccccc",@"ccccc"]]
    NSLog(@"%@",result);

NSPredicateOperatorType枚舉用來設(shè)置運算符類型,如下:

typedef NS_ENUM(NSUInteger, NSPredicateOperatorType) {
    NSLessThanPredicateOperatorType = 0, // 小于
    NSLessThanOrEqualToPredicateOperatorType, // 小于等于
    NSGreaterThanPredicateOperatorType, // 大于
    NSGreaterThanOrEqualToPredicateOperatorType, // 大于等于
    NSEqualToPredicateOperatorType, // 等于
    NSNotEqualToPredicateOperatorType, //不等于
    NSMatchesPredicateOperatorType, //正則比配
    NSLikePredicateOperatorType,  //Like匹配 與SQL類似
    NSBeginsWithPredicateOperatorType, //左邊的表達式 以右邊的表達式作為開頭
    NSEndsWithPredicateOperatorType,//左邊的表達式 以右邊的表達式作為結(jié)尾
    NSInPredicateOperatorType, // 左邊的表達式 出現(xiàn)在右邊的集合中 
    NSCustomSelectorPredicateOperatorType,//使用自定義的函數(shù)來進行 驗證
    NSContainsPredicateOperatorType, //左邊的集合包括右邊的元素
    NSBetweenPredicateOperatorType //左邊表達式的值在右邊的范圍中 例如 1 BETWEEN { 0 , 33 }
};

NSComparisonPredicateOptions枚舉用來設(shè)置比較的方式,如下:

//如果不需要特殊指定  這個枚舉值也可以傳0
typedef NS_OPTIONS(NSUInteger, NSComparisonPredicateOptions) {
    NSCaseInsensitivePredicateOption = 0x01, //不區(qū)分大小寫
    NSDiacriticInsensitivePredicateOption = 0x02,//不區(qū)分讀音符號
    NSNormalizedPredicateOption //比較前進行預(yù)處理 代替上面兩個選項
};

2.NSExpression類

NSExpression類則是提供創(chuàng)建表達式,下面列出了其中一些方便理解的方法:

//通過格式化字符串創(chuàng)建表達式
+ (NSExpression *)expressionWithFormat:(NSString *)expressionFormat argumentArray:(NSArray *)arguments;
+ (NSExpression *)expressionWithFormat:(NSString *)expressionFormat, ...;
+ (NSExpression *)expressionWithFormat:(NSString *)expressionFormat arguments:(va_list)argList;
//直接通過對象創(chuàng)建常亮的表達式
+ (NSExpression *)expressionForConstantValue:(nullable id)obj; 
//創(chuàng)建變量表達式  驗證時將從binding字典中進行替換
+ (NSExpression *)expressionForVariable:(NSString *)string; 
//將多個表達式組合成一個
+ (NSExpression *)expressionForAggregate:(NSArray<NSExpression *> *)subexpressions;
+ (NSExpression *)expressionForUnionSet:(NSExpression *)left with:(NSExpression *)right;
+ (NSExpression *)expressionForIntersectSet:(NSExpression *)left with:(NSExpression *)right;
+ (NSExpression *)expressionForMinusSet:(NSExpression *)left with:(NSExpression *)right;
+ (NSExpression *)expressionForSubquery:(NSExpression *)expression usingIteratorVariable:(NSString *)variable predicate:(NSPredicate *)predicate;
//通過預(yù)定義的函數(shù)和參數(shù)數(shù)組來構(gòu)建表達式對象  預(yù)定義的函數(shù) 可見dev開發(fā)文檔
+ (NSExpression *)expressionForFunction:(NSString *)name arguments:(NSArray *)parameters;

3.NSCompoundPredicate類

這個類也是NSPredicate類的子類,其使用邏輯關(guān)系來組合多個謂詞對象,解析如下:

//進行對象初始化
/*
typedef NS_ENUM(NSUInteger, NSCompoundPredicateType) {
    NSNotPredicateType = 0,   //取非
    NSAndPredicateType,  //與運算 
    NSOrPredicateType,   //或運算
};
*/
- (instancetype)initWithType:(NSCompoundPredicateType)type subpredicates:(NSArray<NSPredicate *> *)subpredicates;
//快速創(chuàng)建與運算
+ (NSCompoundPredicate *)andPredicateWithSubpredicates:(NSArray<NSPredicate *> *)subpredicates;
//快速創(chuàng)建或運算
+ (NSCompoundPredicate *)orPredicateWithSubpredicates:(NSArray<NSPredicate *> *)subpredicates;
//快速創(chuàng)建非運算
+ (NSCompoundPredicate *)notPredicateWithSubpredicate:(NSPredicate *)predicate;

四、謂詞的幾種使用場景

謂詞主要用在驗證對象,數(shù)組和集合的過濾。對象的驗證前面有介紹,關(guān)于數(shù)據(jù)和集合的過濾函數(shù),類別如下:

@interface NSArray<ObjectType> (NSPredicateSupport)
//不可變數(shù)組使用過濾器后返回新數(shù)組
- (NSArray<ObjectType> *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 
@end

@interface NSMutableArray<ObjectType> (NSPredicateSupport)
//可變數(shù)組可以直接進行過濾操作
- (void)filterUsingPredicate:(NSPredicate *)predicate;
@end

@interface NSSet<ObjectType> (NSPredicateSupport)
//不可變集合過濾后返回新集合
- (NSSet<ObjectType> *)filteredSetUsingPredicate:(NSPredicate *)predicate;
@end

@interface NSMutableSet<ObjectType> (NSPredicateSupport)
//可變集合可以直接進行過濾操作
- (void)filterUsingPredicate:(NSPredicate *)predicate;
@end

@interface NSOrderedSet<ObjectType> (NSPredicateSupport)

- (NSOrderedSet<ObjectType> *)filteredOrderedSetUsingPredicate:(NSPredicate *)p;

@end

@interface NSMutableOrderedSet<ObjectType> (NSPredicateSupport)

- (void)filterUsingPredicate:(NSPredicate *)p;

@end

五、謂詞的格式化語法總覽

下面列出了在謂詞的格式化字符串規(guī)則語法。

語法規(guī)則 意義
= 左側(cè)等于右側(cè)
== 左側(cè)等于右側(cè),與=一致
>= 左側(cè)大于等于右側(cè)
=> 左側(cè)大于等于右側(cè) 與 >=一致
<= 左側(cè)小于等于右側(cè)
=< 左側(cè)小于等于右側(cè) 與<=一致
> 左側(cè)大于右側(cè)
< 左側(cè)小于右側(cè)
!= 左側(cè)不等于右側(cè)
<> 左側(cè)不等于右側(cè) 與!=一致
BETWEEN 左側(cè)在右側(cè)的集合中 key BETWEEN @[@1,@2]
TRUEPREDICATE 總是返回YES的謂詞
FALSEPREDICATE 總是返回NO的謂詞
AND 邏輯與
&& 邏輯與 與AND一致
OR 邏輯或
NOT 邏輯非
! 邏輯非 與NOT一致
BEGINWITH 左側(cè)以右側(cè)字符串開頭
ENDWITH 左側(cè)以右側(cè)字符串結(jié)尾
CONTAINS 左側(cè)集合包含右側(cè)元素
LIKE 左側(cè)等于右側(cè) 并且 *和?等通配符可以使用
MATCHES 正則匹配
ANY 對于數(shù)組集合類,驗證其中任一元素
SOME 同ANY一致
ALL 對于數(shù)組集合類,驗證其中所有元素
NONE 作用等同于NOT (ANY)
IN 左側(cè)在右側(cè)集合中
SELF 被驗證的對象本身
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容