Iterators, in fact, are collection traversal methods, usually implemented in a loop, recursively, when creating a collection class you need an iterator

Apple has used NSEnumerator class implements the iterator pattern, and the concrete subclass of the abstract class can be traversing various collections, NSSet, NSDictionary, NSArray, already have we use this, it is not necessary at the time of create a class to create an iterator

For example, in the display method of oszNode. m, I use apple’s native iterator directly to iterate over the names of child nodes

There are two types of iterators: external iterators and internal iterators, which in my opinion is the difference between with and without interfaces, encapsulation and no encapsulation 😂

Multiple ways to use iterators:

#import "OSZNineVC.h" @interface OSZNineVC () @end @implementation OSZNineVC - (void)viewDidLoad { [super viewDidLoad]; // [self test1]; // [self test2]; // [self test3]; [self test4]; // [self test5]; } - (void)test1 {//1. Array iterator NSArray *array = [NSArray arrayWithObjects:@1, @2, @3, @4, @5, nil]; NSEnumerator *enu1 = [array objectEnumerator]; NSEnumerator *enu2 = [array reverseObjectEnumerator]; // set id obj = nil; While (obj = [enu1 nextObject]) {NSLog(@"%@", obj); While (obj = [enu2 nextObject]) {NSLog(@"%@", obj); }} - (void) test2 {/ / 2. Set the iterator NSSet * set = [NSSet setWithObjects: @ 1, @ 2, @ 3, @ 4, @ 5, @ 6, @ 7, @ 6, nil]; NSEnumerator *enu = [set objectEnumerator]; id obj = nil; while (obj = [enu nextObject]) { NSLog(@"%@", obj); }} - (void) test3 {/ / 3. The dictionary iterator NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1," @ "key1", @"value2", @"key2", nil]; NSEnumerator *keyEnumer = [dic keyEnumerator]; id key = nil; while (key = [keyEnumer nextObject]) { NSLog(@"%@ = %@", key, [dic objectForKey:key]); } // 4. NSEnumerator *objEnumer = [dic objectEnumerator]; id obj = nil; while (obj = [objEnumer nextObject]) { NSLog(@"%@", obj); }} - (void) test4 {/ / rapid traverse NSArray * array = @ [@ "zhang SAN," @ "bill," @ "detective"]. for (id item in array) { NSLog(@"%@", item); } //for loop for (int I = 0; i < array.count; i++) { NSLog(@"%@",array[i]); }} - (void)test5 {// Block-based enumeration //1. Array NSArray *array = @[" @3 ", @4 ", @5 "]; NSString * STR = @" STR "; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"item is :%@", Obj); the if ([obj localizedStandardCompare: STR] = = NSOrderedSame) {* stop = YES; NSLog (@ "stop traversal");}}]; / / 2. The dictionary NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1," @ "key1" @ "value2" @ "key2", nil]; [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"item is :%@", obj); }]; //3.set NSSet *set = [NSSet setWithObjects:@1, @2, @3, @4, @5, @6, @7, @6, nil]; [set enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"%@", obj); }]; } @endCopy the code

The composite pattern is a tree design pattern.

Author: oldSix_Zhu links: www.jianshu.com/p/118f70673… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.