NSPredicate是什么?
NSPredicate 是預測的意思 但是我們常翻譯成謂詞
它可以干什么?
使用NSPredicate可以定義模糊查詢條件 根據一定的條件 我們就可以從一個數組中快速找出 符合一定條件的元素對象
本次的示范我們沿用上次講的 NSSortDescriptor 的使用 里面的代碼 我們只需要稍微的修改一下 導航欄右邊的按鈕 為‘搜索年齡大於20的對象’ 然后再把點擊左上角按鈕的業務代碼修改為如下:
- (IBAction)sortAge:(id)sender { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>20"]; //創建NSPredicate對象 並定義查詢條件 self.datas = [[self.datas filteredArrayUsingPredicate:predicate] mutableCopy]; //使用數組的filteredArrayUsingPredicate方法 獲取符合我們指定條件的對象
[self.tableView reloadData];
}
這個時候我們點擊左上角的按鈕 就可以查詢出數組里面 年齡大於20的對象
除了 > 號之外 我們還可以用IN 查詢兩個數組的交集
我們新建一個程序 來測試IN 的用法 我們在ViewDidload方法里面 寫入如下代碼:
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8, nil]; NSArray *array2 = [NSArray arrayWithObjects:@4,@6, nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in%@",array2]; //SELF 代表本身 IN可以大寫也可以小寫
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; //表示獲取 array2 和 array1中的交集
NSLog(@"temp ====\n%@",temp); }
運行 打印結果為:
temp ====
(
4,
6
)
可以看到這兩個數組的交集就是4和6 因為這兩個數組中 都有4和6 這個元素
同時除了IN 之外 BETWEEN可以獲取一定范圍的值 示例代碼如下:
- (void)viewDidLoad { [super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@100,@20,@3,@4,@4,@6,@7,@1, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN{1,20}"]; NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
上面的代碼表示找出 值在1到20范圍類的值包括首尾
打印結果如下:
temp ====
(
20,
3,
4,
4,
6,
7,
1
)
當然除了上面介紹的兩個用法之外 還有其他的比較運算符>,<,==,>=,<=,!=
這里以等於號==為例:
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@100,@20,@3,@4,@4,@6,@7,@1, nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ==6"]; NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
打印結果如下:
temp ====
(
6
)
同時還有以下與字符串操作相關的關鍵詞 :
BEGINSWITH :以某個字符串開頭
ENDSWITH :以某個字符串結尾
CONTAINS :是否包含某個字符串
同時這三個關鍵詞后面還可以跟上一些格式符號 如:BEGINSWITH[cd] c表示不區分大小寫 d表示不區分發音符號 cd就可以表示即不區分大小寫 也不區分發音符號
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil]; //查詢出包含e這個字符的字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] 'E' "]; NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
打印結果如下:
temp ====
(
anne,
reserved,
type,
soure,
version
)
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil]; //查詢出以a這個字符開頭的字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'a' "]; NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
打印結果如下:
temp ====
(
anne
)
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil]; //查詢出以e這個字符結尾的字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH[cd] 'e' "]; NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
打印結果如下:
temp ====
(
anne,
type,
soure
)
還有一個你可能會用到的關鍵字 LIKE 他后面也可以寫[cd]格式符號
- (void)viewDidLoad { [super viewDidLoad]; NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil]; //查詢出包含e這個字符的字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] '*e*' "]; //*表示通配符 NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; NSLog(@"temp ====\n%@",temp); }
打印結果如下:
temp ====
(
anne,
reserved,
type,
soure,
version
)
最后附上所有的相關的條件字符