OC中的謂詞操作是針對于數(shù)組類型的,他就好比數(shù)據(jù)庫中的查詢操作,數(shù)據(jù)源就是數(shù)組,這樣的好處是我們不需要編寫很多代碼就可以去操作數(shù)組,同時也起到過濾的作用,我們可以編寫簡單的謂詞語句,就可以從數(shù)組中過濾出我們想要的數(shù)據(jù)。非常方便。在Java中是沒有這種技術(shù)的,但是有開源的框架已經(jīng)實(shí)現(xiàn)了此功能。
下面來看一下具體的例子吧:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *name;
@property NSInteger age;
+ (id)personWithName:(NSString *)name andAge:(NSInteger)age;
@end
Person.m
#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類中定義屬性,還有一個產(chǎn)生對象的類方法,同時重寫了description方法,用于打印結(jié)果
測試方法
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
//謂詞,指定過濾器的條件,將符合條件的對象保留下來
//一般用謂詞過濾數(shù)組中指定的元素
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]];
//年齡小于30
//定義謂詞對象,謂詞對象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
//使用謂詞條件過濾數(shù)組中的元素,過濾之后返回查詢的結(jié)果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
//查詢name=1的并且age大于40
predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
//in(包含)
predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
//name以a開頭的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
//name以ba結(jié)尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
//name中包含字符a的
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
//like 匹配任意多個字符
//name中只要有s字符就滿足條件
predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
//?代表一個字符,下面的查詢條件是:name中第二個字符是s的
predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
}
return 0;
}
```
首先我們看到,我們初始化了一定大小的數(shù)組。
然后我們就可以使用NSPredicate類進(jìn)行過濾操作了
**1、查詢數(shù)組中年齡小于30的對象**
```
//年齡小于30
//定義謂詞對象,謂詞對象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
//使用謂詞條件過濾數(shù)組中的元素,過濾之后返回查詢的結(jié)果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
```
首先創(chuàng)立一個過濾條件:
```
//定義謂詞對象,謂詞對象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
這里面操作很簡單的:@"age<%d",這個age是Person的屬性名,%d相當(dāng)于占位符,然后后面用參數(shù)替換即可
然后進(jìn)行過濾操作,返回一個過濾之后的數(shù)組對象
``
//使用謂詞條件過濾數(shù)組中的元素,過濾之后返回查詢的結(jié)果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
```
**2、查詢name=1并且age大于40的集合**
```
//查詢name=1的并且age大于40
predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
當(dāng)然我們也可以使用&&進(jìn)行多條件過濾
```
**3、包含語句的使用**
```
//in(包含)
predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
```
**4、指定字符開頭和指定字符結(jié)尾,是否包含指定字符**
```
//name以a開頭的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
//name以ba結(jié)尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
//name中包含字符a的
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
```
**5、like進(jìn)行匹配多個字符**
```
//like 匹配任意多個字符
//name中只要有s字符就滿足條件
predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
//?代表一個字符,下面的查詢條件是:name中第二個字符是s的
predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
```
**6NSCompoundPredicate進(jìn)行匹配多個條件**
```
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", searchString];
// NSLog(@"--->%@",[self.products filteredArrayUsingPredicate:preicate]);
NSPredicate *preicate1 = [NSPredicate predicateWithFormat:@"age CONTAINS[c] %@", searchString];
//
// NSLog(@"--->%@",[self.products filteredArrayUsingPredicate:preicate1]);
// //多個條件
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[preicate, preicate1]];
NSLog(@"====--->%@",[persons filteredArrayUsingPredicate:predicate]);
```