NSArray NSMutableArray 的并发遍历 与 反向遍历

来源:互联网 发布:怎样安装三菱编程软件 编辑:程序博客网 时间:2024/05/17 22:00

NSArray NSMutableArray

http://blog.csdn.net/ysy441088327/article/details/7460200

注: iOS 6 新的快捷初始化写法:

NSArray:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSArray *array = @[@"xiaoyu",@"yushuyi"];  

NSMutableArray:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSMutableArray *MArray = [@[@"xiaoyu",@"yushuyi"] mutableCopy];  

1:使用Block遍历数据

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSArray *array = @[@"11",@"22"];  
  2. [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {  
  3.     NSLog(@"%@ %d",obj,idx);  
  4. }];  

注:为stop赋值为YES 可停止遍历.


2:带参数设定的Block遍历数据

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [mArray enumerateObjectsWithOptions:  usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {  
  2.     NSLog(@"%@",obj);  
  3. }];  

1:NSEnumerationConcurrent 以并发的方式遍历NSArray

2:NSEnumerationReverse     以逆向反向的方式遍历NSArray




3:Array 排序

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSArray *sortedArray = [dayDataMArrray sortedArrayUsingComparator: ^(id obj1, id obj2)  
  2. {  
  3.     NSDate *obj1Date = [NSDate Help_dateWithDateString: obj1 withDateFormat:@"yyyy-MM-dd"];  
  4.     NSDate *obj2Date = [NSDate Help_dateWithDateString: obj2 withDateFormat:@"yyyy-MM-dd"];  
  5.     if ([obj2Date timeIntervalSinceDate:obj1Date] > 0) {  
  6.         return (NSComparisonResult)NSOrderedAscending;  
  7.     }  
  8.     if ([obj2Date timeIntervalSinceDate:obj1Date] < 0) {  
  9.         return (NSComparisonResult)NSOrderedDescending;  
  10.     }  
  11.     return (NSComparisonResult)NSOrderedSame;  
  12. }];  

注:

排序的理由是由开发者自行定义的.

灵活的返回 NSComparisonResult 即可达到任何排序效果.



4:让数组内的元素依次调用同一个方法

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];  

也可以传参数:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;  


版权声明:本文为博主原创文章,未经博主允许不得转载。


0 0