Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basics of fast enumeration.
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Weverything -framework Foundation -o fast-enum-1 fast-enum-1.m
#pragma clang diagnostic ignored "-Wobjc-missing-property-synthesis"
// --------------------------------------------------
// Pass-through
@interface Musician : NSObject
+ (id) musicianWithName: (NSString *) name instrument: (NSString *) instrument;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *instrument;
@end // Musician
@implementation Musician
+ (id) musicianWithName: (NSString *) name instrument: (NSString *) instrument {
Musician *musician = [[self alloc] init];
musician.name = name;
musician.instrument = instrument;
return musician;
} // musicianWithName
@end // Musician
@interface Orchestra : NSObject <NSFastEnumeration>
- (void) addMusician: (Musician *) musician;
@end // Orchestra
@implementation Orchestra {
NSMutableArray *_members;
}
- (void) addMusician: (Musician *) musician {
if (!_members) _members = [NSMutableArray array];
[_members addObject: musician];
} // addMusician
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *) enumerationState
objects: (id __unsafe_unretained []) buffer
count: (NSUInteger) len {
return [_members countByEnumeratingWithState: enumerationState
objects: buffer
count: len];
} // countByEnumeratingWithState
@end // Orchestra
int main (void) {
@autoreleasepool {
NSLog (@"-------------------- Fast enumeration");
NSArray *strings =
[NSArray arrayWithObjects: @"greeble", @"bork", @"hoover", nil];
for (NSString *thing in strings) {
NSLog (@"Woo! %@", thing);
}
NSLog (@"-------------------- Old-timey enumeration");
NSEnumerator *enumerator = [strings objectEnumerator];
NSString *thing;
while ((thing = [enumerator nextObject])) {
NSLog (@"woo. %@", thing);
}
NSLog (@"-------------------- Reverse enumeration");
NSEnumerator *rotaremune = [strings reverseObjectEnumerator];
for (thing in rotaremune) {
NSLog (@"!ooW %@", thing);
}
NSLog (@"-------------------- Directory enumeration");
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [fileManager enumeratorAtPath: @"/usr/share/dict"];
NSString *filename;
while ((filename = [direnum nextObject])) {
NSDictionary *fileAttributes = [direnum fileAttributes];
NSLog (@"file! %@ : %llu", filename, [fileAttributes fileSize]);
}
NSLog (@"-------------------- Directory fast enumeration");
direnum = [fileManager enumeratorAtPath: @"/usr/share/dict"];
for (filename in direnum) {
NSDictionary *fileAttributes = [direnum fileAttributes];
NSLog (@"file! %@ : %llu", filename, [fileAttributes fileSize]);
}
NSLog (@"-------------------- Pass-through fast enumeration");
Orchestra *edgewoodSymphony = [[Orchestra alloc] init];
Musician *peggy = [Musician musicianWithName: @"Peggy" instrument: @"Flaut"];
[edgewoodSymphony addMusician: peggy];
Musician *jim = [Musician musicianWithName: @"Jim" instrument: @"Bassoon"];
[edgewoodSymphony addMusician: jim];
for (Musician *member in edgewoodSymphony) {
NSLog (@"%@ plays the %@", member.name, member.instrument);
}
}
return 0;
} // main
#if 0
% clang -g -fobjc-arc -Weverything -framework Foundation -o fast-enum-1 fast-enum-1.m
% ./fast-enum-1
-------------------- Fast enumeration
Woo! greeble
Woo! bork
Woo! hoover
-------------------- Old-timey enumeration
woo. greeble
woo. bork
woo. hoover
-------------------- Reverse enumeration
!ooW hoover
!ooW bork
!ooW greeble
-------------------- Directory enumeration
file! connectives : 706
file! propernames : 8546
file! README : 1689
file! web2 : 2493109
file! web2a : 1012731
file! words : 4
-------------------- Directory fast enumeration
file! connectives : 706
file! propernames : 8546
file! README : 1689
file! web2 : 2493109
file! web2a : 1012731
file! words : 4
-------------------- Pass-through fast enumeration
Peggy plays the Flaut
Jim plays the Bassoon
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.