Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fast enumeration in Objective-C guarantee the order of iteration?

Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this.

i.e. is

for (id val in array) {    NSLog(@"%@", val); } 

always going to print out the same as

for (int i = 0; i < [array count]; ++i) {    NSLog(@"%@", [array objectAtIndex:i]); } 
like image 567
Ranking Stackingblocks Avatar asked May 06 '10 00:05

Ranking Stackingblocks


People also ask

What is fast enumeration in Objective C?

Fast Enumeration was introduced into Objective-C back in the 10.5 days. It's the feature that lets you succinctly iterate through a collection: NSArray *strings = [NSArray arrayWithObjects: @"greeble", @"bork", @"hoover", nil]; for (NSString *thing in strings) { NSLog (@"Woo!

What is fast enumeration in Swift?

Fast enumeration is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.


1 Answers

From Apples' Objective-C documentation on fast enumeration:

For collections or enumerators that have a well-defined order—such as NSArray or NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations will give you the proper index into the collection if you need it.

like image 129
Georg Fritzsche Avatar answered Oct 02 '22 16:10

Georg Fritzsche