Questions tagged [fast-enumeration]

An Objective-C language feature which offers more concise enumeration code with better performance than other options (i.e. NSEnumerator)

Instead of using an NSEnumerator object or indices to iterate through a collection, Objective-C 2.0 offers the fast enumeration syntax. In Objective-C 2.0, the following loops are functionally equivalent, but have different performance characteristics.

// Using NSEnumerator
NSEnumerator *enumerator = [thePeople objectEnumerator];
Person *p; 
while ((p = [enumerator nextObject]) != nil) {
 NSLog(@"%@ is %i years old.", [p name], [p age]);

// Using fast enumeration
for (Person *p in thePeople) {
 NSLog(@"%@ is %i years old.", [p name], [p age]);
}

Fast enumeration generates more efficient code than standard enumeration because method calls to enumerate over objects are replaced by pointer arithmetic using the NSFastEnumeration protocol

Reference:
http://en.wikipedia.org/wiki/Objective-C#Fast_enumeration

135 questions
6
votes
4 answers

Is it possible to restart a for loop to its first iteration?

In Objective-C, is it possible to restart to the first iteration of a for loop? I don't want to do anything with my objects until I find a "good" object, at which point I want to go back and do stuff with every object up to that good…
A O
  • 5,516
  • 3
  • 33
  • 68
6
votes
4 answers

How do you stop fast enumeration?

How would you stop fast enumeration once you have gotten what your looking for. In a for loop I know you just set the counter number to like a thousand or something. Example: for (int i=0;i<10;i++){ if (random requirement){ random code …
bmende
  • 741
  • 1
  • 11
  • 24
6
votes
3 answers

Example of adopting/implementing fast enumeration for my class?

I'm trying to understand how to adopt the fast enumeration protocol (under iOS/objective C) for a class I'm creating. I read the section of Apple's docs, but... I don't quite get it! Anyone have some sample code I could look at? What I'm trying to…
Anna Dickinson
  • 3,307
  • 24
  • 43
5
votes
5 answers

Fast Enumeration on NSArray of Different Types

I have this question here (as well other quesrtions on SO), and the Apple docs about Objective-C collections and fast enumeration. What is not made clear is if an NSArray populated with different types, and a loop is created like: for ( NSString…
Mike D
  • 4,938
  • 6
  • 43
  • 99
5
votes
1 answer

Is it safe to enumerate through [NSOperationQueue operations]?

Is it safe to enumerate, via fast enumeration, through [NSOperationQueue operations]? Like so: for (NSOperation *op in [operationQueue operations]) { // Do something with op } Since operations are asynchronous and executed on another thread,…
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
5
votes
3 answers

Remove items in a for loop without side effects?

Can I remove items that I am looping through in an Objective-C for loop without side effects? For example, is this ok? for (id item in items) { if ( [item customCheck] ) { [items removeObject:item]; // Is this ok here? }
Greg
  • 34,042
  • 79
  • 253
  • 454
5
votes
1 answer

How for in loop works internally - Objective C - Foundation

I found this answer: https://stackoverflow.com/a/5163334/1364174 Which presents how for in loop is implemented. NSFastEnumerationState __enumState = {0}; id __objects[MAX_STACKBUFF_SIZE]; NSUInteger __count; while ((__count = [myArray…
4
votes
3 answers

fast enumeration for NSDictionary instance ordered by the key

Overview I am using fast enumeration to iterate through an NSDictionary instance I expected the NSDictionary instance to be enumerated based on the ascending order of the key but that doesn't seem to be the case What I want to do: I want to be…
user1046037
  • 16,755
  • 12
  • 92
  • 138
4
votes
8 answers

Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

I thought that I knew how to use fast enumeration, but there is something I don't understand about it. If I create three NSString objects and three NSNumber objects and put them in an NSMutableArray: NSString *str1 = @"str1"; NSString *str2 =…
greg.bzh
  • 63
  • 4
4
votes
3 answers

Why does loop variable become `nil` after loop

I have: NSDictionary* server; for (server in self.servers) { if () { break; } } // If the criterium was never true, I want to use the last item in the // the array. But turns out that `server` is `nil`. The…
4
votes
3 answers

Details of using fast enumeration on a copy of an NSMutableArray to remove objects

I learnt the hard way that you can't remove objects from an NSMutableArray when you are looping through the objects in it. Looping through [< array_object > copy] instead of < array_object > fixes it. However, I have a few unanswered questions that…
4
votes
2 answers

Why is iterating through NSArray is faster than iterating through NSSet?

I was wondering Why is iterating through NSArray is faster than iterating through NSSet? I imaging that it has something to do with the fact that NSArray is ordered while the NSSet is not but I what a certified answer instead of just guessing.…
AmirZ
  • 401
  • 6
  • 12
4
votes
2 answers

Is fast enumeration considered bad form or is it generally accepted?

Just wondering, it seems like it works pretty well but I want to make sure I am using generally accepted coding practices and not get into bad habits. Thanks, Nick
nickthedude
  • 4,925
  • 10
  • 36
  • 51
4
votes
3 answers

Setting objects to nil during fast enumeration

I want to set an object to 'nil' as I enumerate through an array, as follows: for(Object* object in array){ object = nil; } Xcode then tells me 'Fast enumeration variables can't be modified in ARC by default; declare the variable __strong to…
Fitzy
  • 1,871
  • 6
  • 23
  • 40
4
votes
3 answers

Using Objective-C, is there a way to convert a tree into Fast Enumeration?

If there is a tree, which has a rootNode, and it points to left and right for its children nodes (a binary tree), is there a way to convert it into Fast Enumeration as in Objective-C 2.0? So we can do for (id node in [tree allNodes]) { // do…
Jeremy L
  • 3,770
  • 6
  • 41
  • 62
1
2
3
8 9