iOS开发中使用NSEnumerator进行遍历
2018-11-25 本文已影响12人
梁森的简书
可复制代码:
NSDictionary * dic = @{@"name":@"梁森", @"age":@"27"};
NSEnumerator * enumerator = [dic keyEnumerator];
id key;
while ((key = [enumerator nextObject]) != nil) {
id value = dic[key];
NSLog(@"value:%@", value);
}
NSArray * arr = @[@"1", @"2", @"3"];
NSEnumerator * arrEnumerator = [arr objectEnumerator];
id object;
while ((object = [arrEnumerator nextObject]) != nil) {
NSLog(@"object:%@", object);
}
最快、最新的“块枚举法”:
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"idx:%lu---obj:%@",idx, obj);
}];