Fork me on GitHub

iOS中NSArray的过滤

  1. //找出一个数组   
  2.    NSFileManager *fileManager = [NSFileManager defaultManager];  
  3.     NSString *defaultPath = [[NSBundle mainBundle] resourcePath];  
  4.     NSError *error;  
  5.       
  6.     NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]  
  7. //匹配字符串,反回结果, SELF==表示数组中每一个元素  
  8. NSString *match = @"imagexyz-999.png";  
  9.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];  
  10.     NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];  
  11.   
  12. //近似匹配字符串,类似SQL中的语法  
  13. NSString *match = @"imagexyz*.png";  
  14.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];  
  15.     NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];  
  16. //不区分大小写匹配  
  17. NSString *match = @"imagexyz*.png";  
  18.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];  
  19.     NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];  
  20. //正则匹配  
  21. NSString *match = @"imagexyz-\\d{3}\\.png";  
  22.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];  
  23.     NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
 

i have NSArray (datesArray) which is composed to :

(
"2011-11-30",
"2011-11-28",
"2011-11-25"
)

and another NSArray (leadsArray) which composed to :

(
{
   
"date_deadline"="2011-11-30";
    name
="test1";
};
{
   
"date_deadline"="2011-11-28";
    name
="test2";
};
{
   
"date_deadline"="2011-11-25";
    name
="test3";
};
{
   
"date_deadline"="2011-11-28";
    name
="test4";
};
)

then i do that :

NSString*date =[datesArray objectAtIndex:section];
NSPredicate*predicate =[NSPredicate predicateWithFormat:@"date_deadline beginswith %@", date];
NSArray*leads =[leadsArray filteredArrayUsingPredicate:predicate];

return[leads count];

posted on 2012-02-12 18:28  pengyingh  阅读(458)  评论(0)    收藏  举报

导航