šŸ OBJ-C RUNES DIFFICULTY: Apprentice Behavioral

The Iterator

Sequentially traversing ethereal collections.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Subschool: Traversal
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OBJC PATTERN //
@protocol TKSpellbookIterator <NSObject>
- (BOOL)hasNext;
- (id)nextSpell;
@end

@interface TKGrimoireIterator : NSObject <TKSpellbookIterator>
@property (nonatomic, strong) NSArray *spells;
@property (nonatomic, assign) NSInteger currentIndex;
- (instancetype)initWithSpells:(NSArray *)spells;
@end
@implementation TKGrimoireIterator
- (instancetype)initWithSpells:(NSArray *)spells {
    if (self = [super init]) { _spells = spells; _currentIndex = 0; }
    return self;
}
- (BOOL)hasNext { return self.currentIndex < self.spells.count; }
- (id)nextSpell {
    if ([self hasNext]) { return self.spells[self.currentIndex++]; }
    return nil;
}
@end

Iterator provides a safe means to parse a spellbook without exposing its internal representation, a precursor to Objective-C’s native NSFastEnumeration.

Runic Tags: #objc #iterator #behavioral