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
4
votes
2 answers

Is this an inefficient way of using fast enumeration?

I don't entirely understand the details of how fast enumeration works, but compare the following two cases: for(NSObject *object in self.myParent.parentsParents.granfathersMother.cousin.unclesNephew.array) { // do something } vs. NSArray…
Snowman
  • 31,411
  • 46
  • 180
  • 303
4
votes
1 answer

Fast Enumeration slower than for-loop in nested enumeration (with test-results)?

I know there are quite some topics that seem to be about the exact same thing, but I didn't find one that really was about what I wanted. So I was curious and wanted to compare the performance of Fast Enumeration to NSEnumerator and a for loop.…
MeXx
  • 3,357
  • 24
  • 39
3
votes
1 answer

Enumerating over an NSMutableDictionary -- can't access object properties from within the loop

I have an NSMutableDictionary, analyzedPxDictionary, containing a bunch of Pixel objects (a custom class I created). Among other things, Pixel objects contain an NSArray property called rgb. That array will always contain three NSNumber objects,…
maxedison
  • 17,243
  • 14
  • 67
  • 114
3
votes
3 answers

How to enumerate through UITextFields on iOS

Which is the correct way of enumerating through sub views to find text fields? NSMutableArray *mutableTFs = [[NSMutableArray alloc] init]; for (UIView *view in [self.view subviews]) { if ([view isKindOfClass:[UITextField class]]) { …
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
3
votes
1 answer

Iterating Through an NSArray

Can someone help me fix my code here. I am trying to do something simple iterate through the whole array that has NSString's in it convert them to NSIntegers and assign them to a NSInteger variable. for (NSInteger *itemF in myNSArray) { …
Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152
3
votes
2 answers

fast enumeration on NSDictionary fails with "[Waypoint countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance ..."

I have my data in a NSDictionary object where the keys are CGPoints converted to NSValues and the objects are UIColors. Here's the method I'm using to return an object from the dictionary: - (UIColor*) getTemperatureColor2 { NSDictionary*…
Leonard
  • 277
  • 1
  • 4
  • 9
3
votes
2 answers

Need JSON document that is generated to be in same order as objects inserted in NSMutableDictionary in iOS

I am generating a JSON document from an NSMutableDictionary that is composed of keys that point to NSStrings, as well as two keys that point in turn to other NSMutableDictionary's. My problem is that when I output the JSON document, I notice that…
syedfa
  • 2,801
  • 1
  • 41
  • 74
3
votes
4 answers

fast enumeration for removing item in NSMutableArray crash

i have a strange issue , if i remove my item at forin enumeration , it would crash , so like this: for (Obstacle *obstacleToTrack in _obstaclesToAnimate) { //this if else not so important for happening crash if(obstacleToTrack.distance >…
Jeff Wang
  • 183
  • 2
  • 11
3
votes
1 answer

How to safely store objects in extra within countByEnumeratingWithState under ARC?

How can I safely store a couple object instances in NSFastEnumerationState's extra array? I want these items to be retained while the loop is running, then released when the loop is complete. - (NSUInteger)countByEnumeratingWithState:…
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
3
votes
1 answer

Is it safe to delete an NSManagedObject using fast enumeration?

You're not supposed to modify the collection being iterated upon with fast enumeration, but I'm not really sure to what extent that is. The below code has not caused me any problems, but I'm not sure if it was me being lucky. Does anyone have a…
Snowman
  • 31,411
  • 46
  • 180
  • 303
3
votes
3 answers

Big Nerd Ranch Objective C Chapter 17 Challenge - Defining Classes

I'm having trouble working with classes. I have to create "StockHolding" objects that are subclasses of NSObject. I create instance variables and methods. Then I create 3 iterations of the stockholdings complete with names and prices, and I load…
2
votes
1 answer

Quickly Setting Inheritable ACEs on Large Network Filesystems

I have a question regarding ACLs on a network filesystem. Basically, I want to set a permission on a top level folder object that has potentially 100,000+ file and folder objects below it. I want the permission to be inheritable via all sub folders…
Rhys Paterson
  • 73
  • 1
  • 8
2
votes
1 answer

Fast enumeration ordering

Do for (id object in array) { // do something with object } guarantee to return the objects in the order they are put in the array?
Abhinav
  • 37,684
  • 43
  • 191
  • 309
2
votes
1 answer

Check Objective-C generics type in for loop (fast enumeration)

Is there any compiler option to warn that the type in the for-in loop is wrong? NSArray *stringsArray = @[ @"Hello", @"World" ]; for (UIView *wrongType in stringsArray) { NSLog(@"object: %@", wrongType); }
ernesto
  • 1,771
  • 2
  • 18
  • 18
2
votes
4 answers

Can I reuse my pointer after it's been added to a mutable array?

Let's say I've got an array with strings. NSArray *names = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil]; What I want is to initiate objects of some custom class and them add them to a mutable array. I'm using a custom init method that…
1 2
3
8 9