注: iOS 6 新的快捷初始化写法:
NSArray:
NSArray *array = @[@"xiaoyu",@"yushuyi"];
NSMutableArray:
NSMutableArray *MArray = [@[@"xiaoyu",@"yushuyi"] mutableCopy];
1:使用Block遍历数据
NSArray *array = @[@"11",@"22"];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ %d",obj,idx);
}];
注:为stop赋值为YES 可停止遍历.
2:带参数设定的Block遍历数据
[mArray enumerateObjectsWithOptions: usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@",obj);
}];
1:NSEnumerationConcurrent 以并发的方式遍历NSArray
2:NSEnumerationReverse 以逆向反向的方式遍历NSArray
3:Array 排序
NSArray *sortedArray = [dayDataMArrray sortedArrayUsingComparator: ^(id obj1, id obj2)
{
NSDate *obj1Date = [NSDate Help_dateWithDateString: obj1 withDateFormat:@"yyyy-MM-dd"];
NSDate *obj2Date = [NSDate Help_dateWithDateString: obj2 withDateFormat:@"yyyy-MM-dd"];
if ([obj2Date timeIntervalSinceDate:obj1Date] > 0) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([obj2Date timeIntervalSinceDate:obj1Date] < 0) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
注:
排序的理由是由开发者自行定义的.
灵活的返回 NSComparisonResult 即可达到任何排序效果.
4:让数组内的元素依次调用同一个方法
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
也可以传参数:
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;