1.for循环遍历
#pragma mark - for循环遍历
- (void)iteratorWithFor{
//数组
NSArray * array = @[@"1",@"2",@"3",@"4",@"5"];
for (int i = 0; i< array.count; i++) {
NSString *obj = array[i];
NSLog(@"数组:%@",obj);
}
//字典
NSDictionary * dic = @{@"name":@"hehe",@"age":@"20",@"sex":@"1"};
NSArray * arr = [dic allKeys];
for (int i = 0; i< arr.count; i++) {
NSString *key = arr[i];
NSString *obj = [dic objectForKey:key];
NSLog(@"字典:key:%@----obj:%@",key,obj);
}
//集合
NSSet *set = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
for (int i = 0; i<[set allObjects].count; i++) {
NSString *obj = [set allObjects][i];
NSLog(@"集合:%@",obj);
}
//反向遍历
NSArray *arr1 = @[@"1",@"2",@"3",@"4",@"5"];
NSInteger count = arr1.count;
for (NSInteger i = count; i>0;i--) {
if (i ==5) {
NSLog(@"反向遍历:%@",arr1[0]);
continue;
}
NSString *obj = arr1[i];
NSLog(@"反向遍历:%@",obj);
}
}
打印结果:
Snip20170829_19.png2.forin遍历
#pragma mark - Forin遍历
- (void)iteratorWithForin{
//数组
NSArray * array = @[@"1",@"2",@"3",@"4",@"5"];
for (id obj in array) {
NSLog(@"数组--forin遍历:%@",obj);
}
//字典
NSDictionary * dic = @{@"name":@"hehe",@"age":@"20",@"sex":@"1"};
for (id obj in dic) {
NSLog(@"字典--forin遍历:%@",dic[obj]);
}
//集合 无序的
NSSet *set = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
for (id obj in set) {
NSLog(@"集合--forin遍历:%@",obj);
}
//反向遍历
NSArray * array1 = @[@"1",@"2",@"3",@"4",@"5"];
for (id obj in [array1 reverseObjectEnumerator]) {
NSLog(@"数组-反向遍历:%@",obj);
}
}
打印结果:
Snip20170829_18.png3.NSEnumerator遍历
#pragma mark - NSEnumerator遍历
- (void)iteratorWithEnumerator{
//数组
NSArray * array = @[@"1",@"2",@"3",@"4",@"5"];
NSEnumerator *enumerator = [array objectEnumerator];
NSString *obj;
while ((obj = [enumerator nextObject]) != nil) {
NSLog(@"NSEnumerator数组遍历--%@",obj);
}
//字典
NSDictionary * dic = @{@"name":@"hehe",@"age":@"20",@"sex":@"1"};
NSEnumerator *enumrator1 = [dic keyEnumerator];
NSString *key;
while ((key = [enumrator1 nextObject]) != nil) {
NSString *obj = dic[key];
NSLog(@"NSEnumerator字典遍历--%@",obj);
}
//集合
NSSet *set = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
NSEnumerator *enumrator2 = [set objectEnumerator];
NSString *obj2;
while ((obj2 = [enumrator2 nextObject]) !=nil) {
NSLog(@"NSEnumerator集合--%@",obj2);
}
//反向遍历
NSArray * array3 = @[@"1",@"2",@"3",@"4",@"5"];
NSEnumerator *enumrator3 = [array3 reverseObjectEnumerator];
NSString *obj3;
while ((obj3 = [enumrator3 nextObject]) != nil) {
NSLog(@"NSEnumerator反向遍历--%@",obj3);
}
}
打印结果:
Snip20170829_17.png4.Block的遍历
#pragma mark - Block的遍历
- (void)iteratorWithBlock{
//数组
NSArray * array = @[@"1",@"2",@"3",@"4",@"5"];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"Block的数组遍历: obj:%@----idx:%ld",obj,idx);
}];
//字典
NSDictionary * dic = @{@"name":@"hehe",@"age":@"20",@"sex":@"1"};
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"Block的字典遍历: key:%@----obj:%@",key,obj);
}];
//集合 无序的
NSSet *set = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
[set enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"Block的集合遍历: obj:%@",obj);
}];
//反向遍历
NSArray * array1 = @[@"1",@"2",@"3",@"4",@"5"];
[array1 enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"Block的反向遍历: obj:%@----idx:%ld",obj,idx);
}];
}
打印结果:
Snip20170829_16.png总结上面的几种遍历方式:
Block遍历方式(最完美的方式)
1.遍历时可以直接从block中获得需要的所有信息,包括下标、值等。特别相对于字典而言,不需要做多余的编码即可同时获得key和value的值。
2.能够直接修改block中key或者obj的类型为真实类型,可以省去类型转换的工作。
3.对于反向遍历enumerateObjectsWithOptions方式传递参数即可。
forIn遍历方式(紧随block之后)
优: 语法简单代码量少效率最高
缺: 无法获得当前遍历操作所针对的下标
NSEnumerator遍历方式和for遍历方式差不多 ,但是项目中一般很少采用NSEnumerator进行遍历
结论:究竟采用哪种遍历方式,还是要看自己喜欢哪种方式,找到适合自己风格就行
网友评论