生活随笔
收集整理的這篇文章主要介紹了
iOS 谓词的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OC中的謂詞操作是針對于數組類型的,他就好比數據庫中的查詢操作,數據源就是數組,這樣的好處是我們不需要編寫很多代碼就可以去操作數組,同時也起到過濾的作用,我們可以編寫簡單的謂詞語句,就可以從數組中過濾出我們想要的數據。非常方便。在Java中是沒有這種技術的,但是有開源的框架已經實現了此功能。
?
下面來看一下具體的例子吧:
Person.h
?
[objc]?view plaincopy
??#import?<Foundation/Foundation.h>????@interface?Person?:?NSObject????@property?NSString?*name;??@property?NSInteger?age;????+?(id)personWithName:(NSString?*)name?andAge:(NSInteger)age;????@end?? ?
?
Person.m
?
[objc]?view plaincopy
??#import?"Person.h"????@implementation?Person????+?(id)personWithName:(NSString?*)name?andAge:(NSInteger)age{??????Person?*person?=?[[Person?alloc]?init];??????person.name?=?name;??????person.age?=?age;??????return?person;??}????-?(NSString?*)description{??????NSString?*s?=[NSString?stringWithFormat:@"name=%@,age=%ld",_name,_age];??????return?s;??}????@end?? 我們在Person類中定義屬性,還有一個產生對象的類方法,同時重寫了description方法,用于打印結果
?
?
測試方法
main.m
?
[objc]?view plaincopy
??#import?<Foundation/Foundation.h>??#import?"Person.h"????int?main(int?argc,?const?charchar?*?argv[])?{??????@autoreleasepool?{???????????????????NSArray?*persons?=?[NSArray?arrayWithObjects:??????????????????????????????[Person?personWithName:@"mac"?andAge:20],??????????????????????????????[Person?personWithName:@"1"?andAge:30],??????????????????????????????[Person?personWithName:@"2"?andAge:40],??????????????????????????????[Person?personWithName:@"3"?andAge:50],??????????????????????????????[Person?personWithName:@"4"?andAge:60],??????????????????????????????[Person?personWithName:@"5"?andAge:70],??????????????????????????????[Person?personWithName:@"6"?andAge:20],??????????????????????????????[Person?personWithName:@"7"?andAge:40],??????????????????????????????[Person?personWithName:@"8"?andAge:60],??????????????????????????????[Person?personWithName:@"9"?andAge:40],??????????????????????????????[Person?personWithName:@"0"?andAge:80],??????????????????????????????[Person?personWithName:@"10"?andAge:90],??????????????????????????????[Person?personWithName:@"1"?andAge:20]];????????????????????????????????????NSPredicate?*predicate?=?[NSPredicate?predicateWithFormat:@"age<%d",30];??????????????????NSArray?*array?=?[persons?filteredArrayUsingPredicate:predicate];??????????NSLog(@"filterArray=%@",array);????????????????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name='1'?&&?age>40"];??????????array?=?[persons?filteredArrayUsingPredicate:predicate];??????????NSLog(@"filterArray=%@",array);????????????????????????????predicate?=?[NSPredicate?predicateWithFormat:@"self.name?IN?{'1','2','4'}?||?self.age?IN{30,40}"];????????????????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name?BEGINSWITH?'a'"];??????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name?ENDSWITH?'ba'"];????????????????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name?CONTAINS?'a'"];????????????????????????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name?like?'*s*'"];??????????????????predicate?=?[NSPredicate?predicateWithFormat:@"name?like?'?s'"];????????????????????????????????????}??????return?0;??}?? 首先我們看到,我們初始化了一定大小的數組。
?
然后我們就可以使用NSPredicate類進行過濾操作了
?
1、查詢數組中年齡小于30的對象
?
[objc]?view plaincopy
NSPredicate?*predicate?=?[NSPredicate?predicateWithFormat:@"age<%d",30];??NSArray?*array?=?[persons?filteredArrayUsingPredicate:predicate];??NSLog(@"filterArray=%@",array);?? 首先創立一個過濾條件:
?
?
[objc]?view plaincopy
NSPredicate?*predicate?=?[NSPredicate?predicateWithFormat:@"age<%d",30];?? 這里面操作很簡單的:@"age<%d",這個age是Person的屬性名,%d相當于占位符,然后后面用參數替換即可
?
然后進行過濾操作,返回一個過濾之后的數組對象
?
[objc]?view plaincopy
NSArray?*array?=?[persons?filteredArrayUsingPredicate:predicate];?? ?
?
2、查詢name=1并且age大于40的集合
?
[objc]?view plaincopy
predicate?=?[NSPredicate?predicateWithFormat:@"name='1'?&&?age>40"];??array?=?[persons?filteredArrayUsingPredicate:predicate];??NSLog(@"filterArray=%@",array);?? 當然我們也可以使用&&進行多條件過濾
?
?
3、包含語句的使用
?
[objc]?view plaincopy
predicate?=?[NSPredicate?predicateWithFormat:@"self.name?IN?{'1','2','4'}?||?self.age?IN{30,40}"];?? ?
?
4、指定字符開頭和指定字符結尾,是否包含指定字符
?
[objc]?view plaincopy
predicate?=?[NSPredicate?predicateWithFormat:@"name?BEGINSWITH?'a'"];??predicate?=?[NSPredicate?predicateWithFormat:@"name?ENDSWITH?'ba'"];????predicate?=?[NSPredicate?predicateWithFormat:@"name?CONTAINS?'a'"];?? ?
?
5、like進行匹配多個字符
?
[objc]?view plaincopy
predicate?=?[NSPredicate?predicateWithFormat:@"name?like?'*s*'"];??predicate?=?[NSPredicate?predicateWithFormat:@"name?like?'?s'"];?? ?
轉載于:https://www.cnblogs.com/shifu/p/4913339.html
總結
以上是生活随笔為你收集整理的iOS 谓词的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。