簡單瞭解一下--謂詞(NSPredicate)

原文網址:http://blog.csdn.net/jiangwei0910410003/article/details/41923507


OC中的謂詞操作是針對於數組類型的,他就好比數據庫中的查詢操作,數據源就是數組,這樣的好處是我們不需要編寫很多代碼就可以去操作數組,同時也起到過濾的作用,我們可以編寫簡單的謂詞語句,就可以從數組中過濾出我們想要的數據。非常方便。在Java中是沒有這種技術的,但是有開源的框架已經實現了此功能。


下面來看一下具體的例子吧:

Person.h

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  Person.h  
  3. //  46_NSPredicate  
  4. //  
  5. //  Created by jiangwei on 14-10-18.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Person : NSObject  
  12.   
  13. @property NSString *name;  
  14. @property NSInteger age;  
  15.   
  16. + (id)personWithName:(NSString *)name andAge:(NSInteger)age;  
  17.   
  18. @end  


Person.m

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  Person.m  
  3. //  46_NSPredicate  
  4. //  
  5. //  Created by jiangwei on 14-10-18.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import "Person.h"  
  10.   
  11. @implementation Person  
  12.   
  13. + (id)personWithName:(NSString *)name andAge:(NSInteger)age{  
  14.     Person *person = [[Person alloc] init];  
  15.     person.name = name;  
  16.     person.age = age;  
  17.     return person;  
  18. }  
  19.   
  20. - (NSString *)description{  
  21.     NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];  
  22.     return s;  
  23. }  
  24.   
  25. @end  
我們在Person類中定義屬性,還有一個產生對象的類方法,同時重寫了description方法,用於打印結果


測試方法

main.m

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  main.m  
  3. //  46_NSPredicate  
  4. //  
  5. //  Created by jiangwei on 14-10-18.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Person.h"  
  11.   
  12. //謂詞,指定過濾器的條件,將符合條件的對象保留下來  
  13. //一般用謂詞過濾數組中指定的元素  
  14. int main(int argc, const charchar * argv[]) {  
  15.     @autoreleasepool {  
  16.          
  17.         NSArray *persons = [NSArray arrayWithObjects:  
  18.                             [Person personWithName:@"mac" andAge:20],  
  19.                             [Person personWithName:@"1" andAge:30],  
  20.                             [Person personWithName:@"2" andAge:40],  
  21.                             [Person personWithName:@"3" andAge:50],  
  22.                             [Person personWithName:@"4" andAge:60],  
  23.                             [Person personWithName:@"5" andAge:70],  
  24.                             [Person personWithName:@"6" andAge:20],  
  25.                             [Person personWithName:@"7" andAge:40],  
  26.                             [Person personWithName:@"8" andAge:60],  
  27.                             [Person personWithName:@"9" andAge:40],  
  28.                             [Person personWithName:@"0" andAge:80],  
  29.                             [Person personWithName:@"10" andAge:90],  
  30.                             [Person personWithName:@"1" andAge:20]];  
  31.           
  32.         //年齡小於30  
  33.         //定義謂詞對象,謂詞對象中包含了過濾條件  
  34.         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
  35.         //使用謂詞條件過濾數組中的元素,過濾之後返回查詢的結果  
  36.         NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
  37.         NSLog(@"filterArray=%@",array);  
  38.           
  39.         //查詢name=1的並且age大於40  
  40.         predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
  41.         array = [persons filteredArrayUsingPredicate:predicate];  
  42.         NSLog(@"filterArray=%@",array);  
  43.           
  44.         //in(包含)  
  45.         predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
  46.           
  47.         //name以a開頭的  
  48.         predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
  49.         //name以ba結尾的  
  50.         predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
  51.           
  52.         //name中包含字符a的  
  53.         predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
  54.           
  55.         //like 匹配任意多個字符  
  56.         //name中只要有s字符就滿足條件  
  57.         predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
  58.         //?代表一個字符,下面的查詢條件是:name中第二個字符是s的  
  59.         predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  
  60.           
  61.           
  62.           
  63.     }  
  64.     return 0;  
  65. }  
首先我們看到,我們初始化了一定大小的數組。

然後我們就可以使用NSPredicate類進行過濾操作了


1、查詢數組中年齡小於30的對象

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //年齡小於30  
  2. //定義謂詞對象,謂詞對象中包含了過濾條件  
  3. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
  4. //使用謂詞條件過濾數組中的元素,過濾之後返回查詢的結果  
  5. NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
  6. NSLog(@"filterArray=%@",array);  
首先創立一個過濾條件:

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //定義謂詞對象,謂詞對象中包含了過濾條件  
  2. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
這裏面操作很簡單的:@"age<%d",這個age是Person的屬性名,%d相當於佔位符,然後後面用參數替換即可

然後進行過濾操作,返回一個過濾之後的數組對象

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //使用謂詞條件過濾數組中的元素,過濾之後返回查詢的結果  
  2. NSArray *array = [persons filteredArrayUsingPredicate:predicate];  


2、查詢name=1並且age大於40的集合

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //查詢name=1的並且age大於40  
  2. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
  3. array = [persons filteredArrayUsingPredicate:predicate];  
  4. NSLog(@"filterArray=%@",array);  
當然我們也可以使用&&進行多條件過濾


3、包含語句的使用

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //in(包含)  
  2. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  


4、指定字符開頭和指定字符結尾,是否包含指定字符

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //name以a開頭的  
  2. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
  3. //name以ba結尾的  
  4. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
  5.   
  6. //name中包含字符a的  
  7. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  


5、like進行匹配多個字符

[objc] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //like 匹配任意多個字符  
  2. //name中只要有s字符就滿足條件  
  3. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
  4. //?代表一個字符,下面的查詢條件是:name中第二個字符是s的  
  5. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  


總結

這一篇就介紹了OC中常用的技術:謂詞的使用,他用起來很方便的,而且也沒什麼難度,和我們當初在操作數據庫的時候很想,但是他對我們進行過濾操作提供了很大的便捷。


發佈了17 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章