nsarray過濾
1. ios開發中NSArray怎麼用NSPredicate來過濾數組內容
//找出一個數組
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
NSError *error;
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]
//匹配字元串,反回結果, SELF==表示數組中每一個元素
NSString *match = @"imagexyz-999.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
//近似匹配字元串,類似SQL中的語法
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
//不區分大小寫匹配
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
//正則匹配
NSString *match = @"imagexyz-//d{3}//.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
2. ios 比較倆個數組是否有不等的元素
方法一:利用NSPredicate
註:NSPredicate所屬Cocoa框架,在密碼、用戶名等正則判斷中經常用到。
類似於SQL語句
NOT 不是
SELF 代表字元串本身
IN 范圍運算符
那麼NOT (SELF IN %@) 意思就是:不是這里所指定的字元串的值
*/
NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
//過濾數組
NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
/*
結果:
Reslut Filtered Array = (
2,
6,
6
)
*/
/*
方法二:從後往前遍歷數組,然後匹配刪除
*/
int i = (int)[dataArray count]-1;
for(;i >= 0;i --){
//containsObject 判斷元素是否存在於數組中(根據兩者的內存地址判斷,相同:YES 不同:NO)
if([filteredArray containsObject:[dataArray objectAtIndex:i]]) {
[dataArray removeObjectAtIndex:i];
}
}
NSLog(@"Data Array = %@",dataArray);
/*
結果:
Data Array = (
2,
6,
6
)
*/
3. juniper NS25中的web過濾怎麼設置
你需要購買websense這個產商的產品服務才能過濾,Juniper的Netscreen自身不自帶這個功能
4. coredata怎麼實現增序查找
//讀取資料庫文件
- (IBAction)readData{
NSLog(@"讀取數據");
dispatch_async(dispatch_get_main_queue(), ^{
// 初始化一個查詢請求
// NSFetchRequest *request = [[NSFetchRequest alloc] init];
// 設置要查詢的實體
// request.entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managedContext];
//以上代碼簡寫成下邊
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
// 設置排序(按照age降序)
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:sort];
// 設置條件過濾(搜索name中包含字元串"zhang"的記錄,注意:設置條件過濾時,資料庫SQL語句中的%要用*來代替,所以%Itcast-1%應該寫成*zhang*)
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*zhang*"];
// request.predicate = predicate;
// 執行請求
NSError *error = nil;
NSArray *objs = [self.managedContext executeFetchRequest:request error:&error];
if (error) {
[NSException raise:@"查詢錯誤" format:@"%@", [error localizedDescription]];
}
NSLog(@"-----------------------------------");
// 遍歷數據
int index = 0;
for (NSManagedObject *obj in objs) {
NSLog(@"%d---name=%@", index++,[obj valueForKey:@"name"]);
}
for (Student *stu in objs) {
Book *book = stu.book;
NSLog(@"%@---name=%@", stu.name,book.bookName);
}
});
}
5. 怎樣過濾NSString中的特殊字元
字元串過濾一下
1 NSCharacterSet *doNotWant = [NSCharacterSet :@"[]{}(#%-*+=_)\\|~(<>$%^&*)_+ "];
2 tempString = [[tempString : doNotWant]componentsJoinedByString: @""];
1 裡面「」裡面放內要過濾的容字元
6. ios 比較倆個數組是否有不等的元素
方法一:利用NSPredicate
註:NSPredicate所屬Cocoa框架,在密碼、用戶名等正則判斷中經常用到。
類似於SQL語句
NOT 不是
SELF 代表字元串本身
IN 范圍運算符
那麼NOT (SELF IN %@) 意思就是:不是這里所指定的字元串的值
*/
NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
//過濾數組
NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
/*
結果:
Reslut Filtered Array = (
2,
6,
6
)
*/
/*
方法二:從後往前遍歷數組,然後匹配刪除
*/
int i = (int)[dataArray count]-1;
for(;i >= 0;i --){
//containsObject 判斷元素是否存在於數組中(根據兩者的內存地址判斷,相同:YES 不同:NO)
if([filteredArray containsObject:[dataArray objectAtIndex:i]]) {
[dataArray removeObjectAtIndex:i];
}
}
NSLog(@"Data Array = %@",dataArray);
/*
結果:
Data Array = (
2,
6,
6
)
*/
7. ios search controller搜索數組里數組怎麼辦
NSPredicate是什麼NSPredicate:謂詞
字面翻譯是這個意思,但是我覺得謂詞這個詞太難以理解了
NSPredicate的具體用途應該還是過濾,類似於過濾條件之類的,相當於一個主語的謂語,所以說會是謂詞這個名字。(我是這么理解的)
NSPredicate的創建
我們看到創建謂詞使用類方法predicateWithFormat: (NSString*) format,format 里的東西真的
和SQL 的where 條件差不多。
另外,參數format 與NSLog 的格式化模版差不多,如果1 和
188.0 是傳遞過來的參數,你可以寫成如下的形式:
@"pid>%d and height<%f",1,188.0
1.比較運算符 > 、< 、== 、 >= 、<= 、 !=
例:@"number >= 99"
2.邏輯運算符:AND、OR、NOT 這幾個運算符計算並、或、非的結果。
3.范圍運算符:IN 、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','nanjing'}"
4.字元串本身:SELF
例:@"SELF == 'APPLE'"
5.字元串相關:BEGINSWITH、ENDSWITH、CONTAINS
例: @"name CONTAIN[cd] 'ang'" //包含某個字元串
@"name BEGINSWITH[c] 'sh'" //以某個字元串開頭
@"name ENDSWITH[d] 'ang'" //以某個字元串結束
注:[c]不區分大小寫 , [d]不區分發音符號即沒有重音符號 , [cd]既不區分大小寫,也不區分發音符號。
6.通配符:LIKE
例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
7.正則表達式:MATCHES
例:NSString *regex = @"^A.+e$"; //以A開頭,e結尾
@"name MATCHES %@",regex
NSPredicate我暫時都是用於過濾的,否則我們過濾就需要自己寫判斷 寫循環,去判斷輸出了。
接下來就上代碼和例子吧。
建立一個實體類 Dog
@property ( , nonatomic) NSString* name;@property (assign , nonatomic) int age ;
這樣我們就有一個類了
我們創建 5隻Dog的實體
Dog * dog2=[Dog news];dog2.name=@"這是第二隻Dog";dog2.age=@"22";
Dog * dog3=[Dog news];dog3.name=@"這是第三隻Dog";dog3.age=@"33";
Dog * dog4=[Dog news];dog4.name=@"這是第四隻Dog";dog4.age=@"44";
Dog * dog5=[Dog news];dog5.name=@"這是第五隻Dog";dog5.age=@"55";
不要吐槽我的Dogs的年齡 → 。→
然後我們需要一個Array把他們都裝進去
醬紫所有的Dog都裝到一個數組里了
使用NSPredicate的篩選
- 沒有使用NSPredicate的篩選應該大家都會寫,循環+判斷。。。
- 使用NSPredicate的篩選
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name=='這是第一隻Dog' AND age ==11"];
for(Dog * d in dogArr)
{
if([predicate evaluateWithObject:d])
{
NSLog(d.name);
}
}
以上的代碼列印出來的結果就是 這是第一隻Dog
NSPredicate篩選一個對象邏輯還是很清晰的,不過這里比較麻煩的就是 還有一個循環,這里我們會發現數組提供了一個方法可以更方便的檢索數組對象直接篩選出一個符合謂詞的新數組。
NSPredicate *pre = [NSPredicate predicateWithFormat:@"age >11"];NSMutableArray *arrayPre=[dogArr filteredArrayUsingPredicate: pre];
arrayPre裡面的對象就會是 除了dog1之外的所有的dog。(至於為什麼看我的條件...)
看到這里發現NSPredicate 可以用於自定義的實體的檢索、篩選,那如果用於 NSString,NSInteger,bool呢?那我們再看一段代碼吧NSArray *arrays=[NSArray arrayWithObjects: @"Apple", @"Google", @"MircoSoft", nil];NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF=='Apple'"];
上面這段代碼是匹配字元串的時候 NSPredicate 需要如何聲明
使用NSPredicate在兩個數組之間進行差異篩選NSArray* array = @[@"aa",@"bb"];NSArray* array2 = @[@"aa",@"bb",@"cc",@"dd"];
NSPredicate* thePredicate = [NSPredicate predicateWithFormat:@"NOT(SELF in %@)",array];
NSArray* arr3 = [array2 filteredArrayUsingPredicate:thePredicate];
NSLog(@"%@",arr3);
上面的代碼輸出結果 arr3={@"cc" ,@"dd"}
這樣大家就很容易理解了
SELF在前面有介紹過 可以往回翻閱一下
NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([pre evaluateWithObject: @"Apple"]){
printf("YES ");
}else{
printf("NO ");
}關於NSPredicate的其他說明和注意事項,以及技巧
- 動態屬性名
假如你的代碼如下
NSPredicate *p = [NSPredicate predicateWithFormat:@"name = %@", @"name1"];顯然代碼沒有任何問題,但是這個不是最好的寫法我建議如下寫法:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
@"name1", @"NAME",nil];
NSPredicate *pre=[preTemplate : dic];
這樣看上去可能會讓代碼邏輯更清晰。
當過濾條件欄位都是動態的時候
NSString *key = @"name";NSString *value = @"name1";
NSPredicate *p = [NSPredicate predicateWithFormat:@"%@ = %@", key, value];
然後當你執行到第三行的時候代碼就會報錯!
邏輯上沒錯誤啊!!!為什麼會出錯呢?
NSPredicate要自動添加引號,所以最後得到的格式應該是@"'name' = 'name1'"。明顯不對。要做的就是:
8. xcode 如何獲取文件夾下文件數目
使用 NSFileManager 類的
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
方法可得到指定路徑下所有文件列表,之後可以獲回得文件數量,過濾指答定文件類型等。