NSEnumerator简单初探

一、简介

像NSArray、NSSet、NSDictionary等集合类均可获取到NSEnumerator, 该类是一个抽象类,没有用来创建实例的公有接口。NSEnumerator的nextObject方法可以遍历每个集合元素,结束返回nil,通过与while结合使用可遍历集合中所有项。

二、使用解析

1、NSArray中使用到的NSEnumerator,代码如下:

- (void)addArrayBtn{
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(0, 200, 300, 50);
    CGPoint btnCenter = CGPointMake(self.view.center.x, btn.frame.origin.y + 25);
    btn.center = btnCenter;
    btn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn];
    [btn setTitle:@"NSArray中NSEnumerator用法" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didClickArrayBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didClickArrayBtn{
    /*
     - (NSEnumerator *)objectEnumerator;       //正向遍历数组
     - (NSEnumerator *)reverseObjectEnumerator;//反向遍历数组
     */
    NSArray *array = @[@"hehe",@"xixi",@"heihei",@"wuwu",@"lala",@"haha"];
    NSEnumerator *enumerator = [array objectEnumerator];
    NSString *obj;
    while (obj = [enumerator nextObject]) {
        NSLog(@"数组正序值开始打印:%@\n",obj);
    }
    NSEnumerator *reverseEnumerator = [array reverseObjectEnumerator];
    id reverseObj;
    while (reverseObj = [reverseEnumerator nextObject]) {
        NSLog(@"数组逆序值开始打印:%@\n",reverseObj);
    }
}

2、NSDictionary中NSEnumerator的使用,代码如下:

- (void)addDictionaryBtn{
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(0, 300, 300, 50);
    CGPoint btnCenter = CGPointMake(self.view.center.x, btn.frame.origin.y + 25);
    btn.center = btnCenter;
    btn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn];
    [btn setTitle:@"NSDictionary中NSEnumerator用法" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didClickDictionaryBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didClickDictionaryBtn{
    /*
     - (NSEnumerator *)keyEnumerator;//获取所有key值
     - (NSEnumerator *)objectEnumerator;//获取所有value值
     */
    
    NSDictionary *dict = @{@"name":@"zhangsan",@"age":@(18),@"height":@(173.5)};
    NSEnumerator *keyEnumerator = [dict keyEnumerator];
    NSString *key;
    while (key = [keyEnumerator nextObject]) {
        NSLog(@"字典key值开始打印:%@\n",key);
    }
    NSEnumerator *objEnumerator = [dict objectEnumerator];
    id obj;
    while (obj = [objEnumerator nextObject]) {
        NSLog(@"字典value值开始打印:%@\n",obj);
    }
}

3、NSSet中NSEnumerator的使用,代码如下:

- (void)addSetBtn{
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(0, 400, 300, 50);
    CGPoint btnCenter = CGPointMake(self.view.center.x, btn.frame.origin.y + 25);
    btn.center = btnCenter;
    btn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn];
    [btn setTitle:@"NSSet中NSEnumerator用法" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didClickSetBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didClickSetBtn{
    /*
     - (NSEnumerator *)objectEnumerator; //获取所有元素的值,并且是无序的
     */
    NSSet *set = [[NSSet alloc]initWithObjects:@"hehe",@"xixi",@"heihei",@"wuwu",@"lala",@"haha",nil];
    NSEnumerator *enumerator = [set objectEnumerator];
    
    id obj;
    while (obj = [enumerator nextObject]) {
        NSLog(@"NSSet中value值开始打印:%@\n",obj);
    }
}

4、看打印效果,在ViewController中添加这三个Btn即可点击后打印:
代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addArrayBtn];
    [self addDictionaryBtn];
    [self addSetBtn];
}

打印效果:

NSEnumerator简单初探_第1张图片
Snip20170505_70.png
三、其他扩展

1、针对于NSEnumerator这个类文件相关内容如下

typedef struct {
    unsigned long state;
    id __unsafe_unretained _Nullable * _Nullable itemsPtr;
    unsigned long * _Nullable mutationsPtr;
    unsigned long extra[5];
} NSFastEnumerationState;

@protocol NSFastEnumeration

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained _Nullable [_Nonnull])buffer count:(NSUInteger)len;

@end

@interface NSEnumerator : NSObject 

- (nullable ObjectType)nextObject;

@end

@interface NSEnumerator (NSExtendedEnumerator)

@property (readonly, copy) NSArray *allObjects;

@end

其中有一个协议NSFastEnumeration,实现这个协议的方法就可以实现快速遍历,集合类都遵守了此协议,所以集合都能够实现快速遍历,具体该怎么使用请看贴文

你可能感兴趣的:(NSEnumerator简单初探)