系统框架--48:多用块枚举,少用for循环

2018-08-12  本文已影响0人  皆为序幕_

for循环

// NSArray(有序)
    NSArray *arr = @[@"1",@"2",@"3"];
    for (int i = 0; i< arr.count;i++) {
        NSLog(@"arrayContent--%@",arr[i]);

    }
   
 // NSDictionary(无序)
    NSDictionary *dic = @{@"1":@"1",@"2":@"2",@"3":@"3"};
    for(int i = 0; i< dic.count;i++)
    {
        NSString *key = dic.allKeys[I];
        NSString *obj = dic[key];
        NSLog(@"dicContent--%@",obj);
    }
    
//NSSet(无序)
    NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3", nil];
    NSArray *setArr = [set allObjects];
    for(int i = 0;i< setArr.count;i++){
        NSString *obj = setArr[i];
        NSLog(@"setContent--%@",obj);
    }

NSEnumerator(不论遍历哪种collection,都可以采用这套相似语法)

  //NSArray(有序)
    NSArray *arr = @[@"1",@"2",@"3"];
    NSEnumerator *enumeratorArr = [arr objectEnumerator];
    id objectArr;
    while ((objectArr = [enumeratorArr nextObject]) != nil) {
        NSLog(@"str:%@",objectArr);
    }

  //NSDictionary(无序)
   NSDictionary *dic = @{@"1":@"1",@"2":@"2",@"3":@"3"};
    NSEnumerator *enumeratorDic = [dic keyEnumerator];
    id key;
    while ((key = [enumeratorDic nextObject]) != nil) {
        NSLog(@"str:%@",dic[key]);
    }

  //NSSet(无序)
    NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3", nil];
    NSEnumerator *enumeratorSet = [set objectEnumerator];
    id setStr;
    while ((setStr = [enumeratorSet nextObject]) != nil) {
        NSLog(@"str:%@",setStr);
    }
    
  //NSArray(反序)
    NSArray *arr = @[@"1",@"2",@"3"];
    NSEnumerator *enumeratorRever = [arr reverseObjectEnumerator];
    id strR;
    while ((strR = [enumeratorRever nextObject]) != nil) {
        NSLog(@"str:%@",strR);
    }

快速遍历

//NSArray
  NSArray *arr = @[@"1",@"2",@"3"];
  for (NSString *str in arr) {
        NSLog(@"arrayContent--%@",str);
    }
    
//NSDictionary
  NSDictionary *dic = @{@"1":@"1",@"2":@"2",@"3":@"3"};
  for (NSString *key in dic) {
        NSString *obj = dic[key];
        NSLog(@"dic--%@",obj);
    }

//NSSet
   NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3", nil];
    for (NSString *str in set) {
        NSLog(@"set--%@",str);
    }

//NSArray
    NSArray *arr = @[@"1",@"2",@"3"];
    for (NSString *str in [arr reverseObjectEnumerator]) {
        NSLog(@"arrayContent--%@",str);
    }

基于块的遍历方式(可以提供遍历时的下标、字典的key与value等)

//NSArray
NSArray *arr = @[@"1",@"2",@"3"];
[arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"arrayContent--%@",obj);
        if (stop) {
            NSLog(@"第%ld遍历",idx);
        }
    }];

//NSDictionary
 NSDictionary *dic = @{@"1":@"1",@"2":@"2",@"3":@"3"};
 [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"dicContetn-key:%@,obj:%@,stop:%d",key,obj,stop);
    }];

//NSSet
NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3", nil];
[set enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"str:%@",obj);
    }];

通过设置NSEnumerationOptions可以选择类型

NS_OPTIONS(NSUInteger, NSEnumerationOptions) {
    NSEnumerationConcurrent = (1UL << 0),//每次迭代并行
    NSEnumerationReverse = (1UL << 1),//反向遍历
};
[arr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"arrayContent--%@",obj);

    }];

[dic enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"dicContetn-key:%@,obj:%@,stop:%d",key,obj,stop);

    }];
上一篇下一篇

猜你喜欢

热点阅读