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

Enumeration and removing a particular object from NSMutableArray

I'm having trouble removing items from my NSMutableArray. I'm extremely new to Objective-C, so please bear with me. So far, I have the following: I'm trying to remove a line from the array if it has certain text inside. I cannot do this while…
Phill
  • 21
  • 2
2
votes
1 answer

Objective-C Fast Enumeration Bubble Sort

I'm trying to integrate some GCD into my code, and have found that a severe bottleneck is a bubble comparison I am performing between objects in a large array. Here is the original code: NSUInteger count = [arrayToDoWorkOn count]; for (int i = 0; i…
Grimless
  • 1,268
  • 1
  • 15
  • 30
2
votes
1 answer

Cookie deletion while enumerating: safe?

Given this code: NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in cookieStorage.cookies) { if (/* my specific condition that is true multiple times */) { [cookieStorage…
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
2
votes
4 answers

Strange for loops I'm not familiar with: "for (id * in *)"

I apologize if this question is exceedingly simple, but I've Googled like crazy and am unable to find a suitable explanation for what this is. for (id line in self.lines){ [linesCopy addObject:[line copyWithZone:zone]]; } I'm just learning…
Rob
  • 1,045
  • 13
  • 28
2
votes
3 answers

Fast enumeration with NSDictionary holding NSDictionary objects

I have an NSDictionary with four objects. Each object is an NSDictionary containing thousands of objects. I have verified through logging of the description of the top level dictionary that it contains what it is supposed to. However, when I run the…
Jim
  • 5,940
  • 9
  • 44
  • 91
2
votes
1 answer

fast enumeration for array containing different types of objects

If I have an NSMutableArray where I added objects of different classes (e.g. NSString, NSMutableString, NSProcessInfo, NSURL, NSMutableDictionary etc.) Now I want to fast enumerate this array, so I tried: for (id *element in mutableArray){ NSLog…
Dev
  • 6,207
  • 4
  • 27
  • 32
2
votes
3 answers

Fast enumeration on a class object

I'm implementing an application where both instances of a class as well as the class itself have "children" (placed inside a NSMutableArray). It's a pretty complicated application, but thanks to Objective-C, it's a breeze: classes are themselves…
Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
1
vote
1 answer

Fast Enumeration on NSArray of string literals

Using ARC... NSArray *array = [NSArray arrayWithObjects:@"dog", @"cat", @"mouse", nil]; for(NSString *animal in array) { NSLog(@"animal = %@", animal); } Yields an index out of bounds error. *** -[__NSArrayM objectAtIndex:]: index 0 beyond…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
1
vote
2 answers

Access objects of a specific type using for-in loop in Objective C

I am running a for-in loop over an NSMutableArray. There are instances of Class A in the array also out of those some are actually instances of its subclass B. So If I only want members of subclass B, I am checking the class of each object I get in…
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
1
vote
1 answer

can NSPredicates be used to replace objects in an array with values from a dictionary?

If I had an NSDictionary like this: NSMutableDictionary *valuesDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithDouble:-60.0],@”a”, [NSNumber numberWithDouble:0.0],@”b”, [NSNumber…
Dave Kliman
  • 441
  • 4
  • 17
1
vote
4 answers

Better solution for this 2x fast-enumeration?

I'm looping through an array and comparing the objects tag property in this array with the objects in another array. Here's my code: NSArray *objectsArray = ...; NSArray *anotherObjectArray = ...; NSMutableArray *mutableArray = ...; for (ObjectA…
runmad
  • 14,846
  • 9
  • 99
  • 140
1
vote
1 answer

How do I detect if an object implements NSFastEnumeration protocol in Objective-C?

I want to use fast enumeration on an object of type id. Basically I'm missing the check here: id object = ; if( ) for (id item in idobject) …
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
1
vote
3 answers

Why won't this simple 'if' statement work (inside fast enumeration)?

I am enumerating through the ChecklistItem entities in my table to see which ones have a priority (NSNumber attribute) of 1. checklistItems are in a to-many relationship with Checklist. In this simple code, the first NSLog works fine, and reports…
Ric Levy
  • 966
  • 1
  • 15
  • 33
1
vote
1 answer

Printout for fast enumeration

What lines of code allow us to print the iterations through fast enumeration without repeating the prefatory statement? My code is the following: #import int main(int argc, const char * argv[]) { @autoreleasepool { …
Eric
  • 893
  • 10
  • 25
1
vote
1 answer

Unpacking packed primitives (such as an enum) from NSArray or NSDictionary during fast enumeration

You can put primitives in an NSArray or NSDictionary by packing them with the @() syntax. For example: typedef enum { MyEnumOne, MyEnumTwo } MyEnum NSDictionary *dictionary = @{ @(MyEnumOne) : @"one", …
1 2 3
8 9