//
// main.m
// NSArray初始化的三种方法和遍历NSArray的三种方法
//
// Created by apple on 15/9/18.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
//方法一: 用for循环遍历NSArray数组,用常规方法定义数组
NSArray *arr1=@[@"a",@"b",@"c"];
for (int i=0; i
NSLog(@"%@",[arr1 objectAtIndex:i]);
}
//方法二:用 for-in循环遍历数字NSArray数组,用构造方法创建数组
NSArray *arr2=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
for (NSString *v in arr2){
NSLog(@"%@",v);
}
//方法三:用block遍历
//-(void)enumerateObjectsUsingBlock:(void(^)(id obj,NSUInteger idx,BOOL * stop))block NS_AVAILABLE(10_6,4_0);
NSArray *arr3=[@"1,2,3,4,5,6,7,8,9" componentsSeparatedByString:@","];
[arr3 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%lu=%@",idx,obj);
}];
}
return 0;
}