05 Objective C数组的四种遍历方法总结

526 阅读5分钟

摘要: objective-c 语言 数组遍历的4种方式: 1、普通for循环; 2、快速for循环; 3、特性block方法; 4、枚举方法。

###Blog类:

// Blog.h
#import <Foundation/Foundation.h>

@interface Blog : NSObject

/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 内容 */
@property (nonatomic, copy) NSString *content;

+ (Blog *)blog;
- (Blog *)setBlogTitle:(NSString *)title andContent:(NSString *)content;

@end

// Blog.m
#import "Blog.h"

@implementation Blog

+ (Blog *)blog {
    Blog *blog = [[Blog alloc] init];
    return blog;
}

- (Blog *)setBlogTitle:(NSString *)title andContent:(NSString *)content {
    _title = title;
    _content = content;
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"blog : title is \"%@\" , and content is \"%@\"", _title,_content ];
}

- (void)dealloc {
    NSLog(@"%@被销毁了",self.title);
}

@end

###主函数:

#pragma mark - Array数组的四种遍历方法

void testArray(){
    Blog *blog1 = [[Blog blog] setBlogTitle:@"Love" andContent:@"I love you"];
    Blog *blog2 = [[Blog blog] setBlogTitle:@"Friendship" andContent:@"you are my best friend"];
    NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",blog1,blog2, nil];
    
    // 第一种遍历:普通for循环
    NSLog(@"第一种遍历:普通for循环");
    long int count = [array count];
    for (int i = 0 ; i < count; i++) {
        NSLog(@"1遍历array: %zi-->%@",i,[array objectAtIndex:i]);
    }
    
    // 第二种遍历:快速for循环,需要有外变量i
    NSLog(@"第二种遍历:快速for循环");
    int i = 0;
    for (id obj in array) {
        NSLog(@"2遍历array:%zi-->%@", i, obj);
        i++;
    }
    
    // 第三种遍历:OC自带方法enumerateObjectsUsingBlock:
    NSLog(@"第三种遍历:Block遍历");
    // 默认为正序遍历
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"3遍历array:%zi-->%@", idx, obj);
    }];
    // NSEnumerationReverse参数为倒序遍历
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"4倒序遍历array:%zi-->%@",idx,obj);
    }];
    
    // 第四种遍历:利用枚举
    NSLog(@"第四种遍历:枚举遍历");
    NSEnumerator *en = array.objectEnumerator;
    id obj;
    int j = 0 ;
    while (obj = [en nextObject]) {
        NSLog(@"5遍历array:%d-->%@",j,obj);
        j++;
    }
}

int main() {
    @autoreleasepool {
        testArray();
    }
    return 0;
}

###打印结果

2017-02-15 10:30:41.899067 01-遍历[52934:3274633] 第一种遍历:普通for循环
2017-02-15 10:30:41.899598 01-遍历[52934:3274633] 1遍历array: 0-->hello
2017-02-15 10:30:41.899661 01-遍历[52934:3274633] 1遍历array: 1-->world
2017-02-15 10:30:41.899763 01-遍历[52934:3274633] 1遍历array: 2-->blog : title is "Love" , and content is "I love you"
2017-02-15 10:30:41.899816 01-遍历[52934:3274633] 1遍历array: 3-->blog : title is "Friendship" , and content is "you are my best friend"
2017-02-15 10:30:41.899835 01-遍历[52934:3274633] 第二种遍历:快速for循环
2017-02-15 10:30:41.899875 01-遍历[52934:3274633] 2遍历array:0-->hello
2017-02-15 10:30:41.899894 01-遍历[52934:3274633] 2遍历array:1-->world
2017-02-15 10:30:41.899918 01-遍历[52934:3274633] 2遍历array:2-->blog : title is "Love" , and content is "I love you"
2017-02-15 10:30:41.899939 01-遍历[52934:3274633] 2遍历array:3-->blog : title is "Friendship" , and content is "you are my best friend"
2017-02-15 10:30:41.899955 01-遍历[52934:3274633] 第三种遍历:Block遍历
2017-02-15 10:30:41.899978 01-遍历[52934:3274633] 3遍历array:0-->hello
2017-02-15 10:30:42.297084 01-遍历[52934:3274633] 3遍历array:1-->world
2017-02-15 10:30:42.297118 01-遍历[52934:3274633] 3遍历array:2-->blog : title is "Love" , and content is "I love you"
2017-02-15 10:30:42.297138 01-遍历[52934:3274633] 3遍历array:3-->blog : title is "Friendship" , and content is "you are my best friend"
2017-02-15 10:30:42.297155 01-遍历[52934:3274633] 4倒序遍历array:3-->blog : title is "Friendship" , and content is "you are my best friend"
2017-02-15 10:30:42.297172 01-遍历[52934:3274633] 4倒序遍历array:2-->blog : title is "Love" , and content is "I love you"
2017-02-15 10:30:42.297186 01-遍历[52934:3274633] 4倒序遍历array:1-->world
2017-02-15 10:30:42.297198 01-遍历[52934:3274633] 4倒序遍历array:0-->hello
2017-02-15 10:30:42.297211 01-遍历[52934:3274633] 第四种遍历:枚举遍历
2017-02-15 10:30:42.297675 01-遍历[52934:3274633] 5遍历array:0-->hello
2017-02-15 10:30:42.297694 01-遍历[52934:3274633] 5遍历array:1-->world
2017-02-15 10:30:42.297715 01-遍历[52934:3274633] 5遍历array:2-->blog : title is "Love" , and content is "I love you"
2017-02-15 10:30:42.297733 01-遍历[52934:3274633] 5遍历array:3-->blog : title is "Friendship" , and content is "you are my best friend"
2017-02-15 10:30:42.297767 01-遍历[52934:3274633] Friendship被销毁了
2017-02-15 10:30:42.297819 01-遍历[52934:3274633] Love被销毁了