在使用過程中有這么一個需求,需要剔除實體某個屬性的值不包含某個字符串,查資料后代碼如下:
request.predicate= [NSPredicate predicateWithFormat:@"!(sessionId CONTAINS[cd] %@)",@"nice"];
如上:sessionId這個屬性不包含nice這個字符串。
NSPredicate字符串比較的其他語法:
1、BEGINSWITH:左邊的表達式以右邊的表達式作為開始。
(例:request.predicate= [NSPredicate predicateWithFormat:@"(屬性 BEGINSWITH[cd] %@)",@"開頭的字符串"];)
2、CONTAINS:左邊的表達式包含右邊的表達式。
(例:request.predicate= [NSPredicate predicateWithFormat:@"(屬性 CONTAINS[cd] %@)",@"包含的字符串"];)
3、ENDSWITH:左邊的表達式以右邊的表達式作為結束。
(例:request.predicate= [NSPredicate predicateWithFormat:@"(屬性 ENDSWITH[cd] %@)",@"結束的字符串"];)
4、LIKE:左邊的表達式等于右邊的表達式:?和*可作為通配符,其中?匹配1個字符,*匹配0個或者多個字符。
(例1:request.predicate= [NSPredicate predicateWithFormat:@"(屬性 LIKE[cd] %@)",@"zsz_???_iOS"]; // 屬性等于zsz_開頭中間,3個任意字符,_iOS結尾的字符串)
(例2:request.predicate= [NSPredicate predicateWithFormat:@"(屬性 LIKE[cd] %@)",@"zsz_*_iOS_*"]; // 屬性等于zsz_開頭中間+任意字符+_iOS+任意字符的字符串)
5、MATCHES:左邊的表達式根據ICU v3(更多內容請查看ICU User Guide for Regular Expressions)的regex風格比較,等于右邊的表達式。
詳細請看參考鏈接:
1、http://nshipster.cn/nspredicate/
2、http://perfectshen.github.io/2016/03/13/NSPredicate/