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
0
votes
0 answers

iOS are fast enumeration collection statement executed multiple times?

Both variants are exactly the same right? Variant 1: for(id obj in [self.instances copy]) { ... } Variant 2: NSArray *instancesCopy = [self.instances copy]; for(id obj in instancesCopy) { ... } Will…
Buju
  • 1,546
  • 3
  • 16
  • 27
0
votes
0 answers

Does enumerateObjectsUsingBlock always always iterates sequentially?

[array enumerateObjectsUsingBlock:^(NSDictionary *tool, NSUInteger idx, BOOL *stop) { NSLog("Idx :: %d",idx); }]; Will the log print indexes sequentially starting from 0 ... till end? If YES, How is it faster than fast enumeration of kind for…
TechnoGeezer
  • 197
  • 2
  • 2
  • 14
0
votes
1 answer

Getting Warning with Parse.com

I'm triying to access each item on an NSArray trough enumerateObjectsUsingBlock, since it let me use fast enumration and evaluating the index. When I use findObjectsInBackgroundWithBlock, I get Warning: A long-running operation is being executed…
outime
  • 135
  • 1
  • 8
0
votes
1 answer

Objective-C How do I enumerate a dictionary with letters and positions

I think i blew up my brain. I have a dictionary with two arrays: letters and numbers. Numbers are the letters' positions on a board. How can I enumerate over these arrays so that: on a board of 64 squares, letter goes on its board number, and other…
ICL1901
  • 7,632
  • 14
  • 90
  • 138
0
votes
1 answer

Transform enumerateObjectsUsingBlock to Fast Enemuration - Swift

I'm wondering how to transform the following below into Swift. I have attempted it, but have gotten stuck on a concept: I am looping through all my objects with my attribute UICollectionViewLayoutAttributes [newVisibleItems…
Fudgey
  • 3,793
  • 7
  • 32
  • 53
0
votes
3 answers

For loop won't execute

The following code doesn't loop: -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"Inside method, before for loop"); NSLog(@"dayBarArray.count = %lu", (unsigned long)dayBarArray.count); for (int i = 0; i <…
rattletrap99
  • 1,469
  • 2
  • 17
  • 36
0
votes
3 answers

Fast Enumeration on Array with similar Objects

I am quite confused on doing fast enumeration with array filled with similar objects Suppose : I have 1 class (Stock Class), it has 1 subClass ForeignStock. Properties of Stock Class : @property float purchaseSharePrice,currentSharePrice; @property…
0
votes
0 answers

Objective-C: How to count for how many times a string appears in an array?

I know this has been asked in a java question. But how can I could in Objective-C count how many times a string appears in an array? For example, (please excuse the faux code here) if I had an array like... index 0 : "Andy" index 1 : "Paul" index 2…
Tom Testicool
  • 563
  • 7
  • 26
0
votes
2 answers

Is it advisable to type the object in an array block enumeration whose type is known?

If I know what type of objects I am storying in my array, is it better to type it as such in the block enumeration or no? Instead of: [myClassArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { }]; Use this: [myClassArray…
Boon
  • 40,656
  • 60
  • 209
  • 315
0
votes
2 answers

using nested fast enumeration takes too long, how to optimize?

I am doing this is soon as the app starts, luckily I have to do it only once in a singleton class called CMIDataManager, my app is taking too long to launch. The plist contains: Commanders.plist: German - Array Soviet - Array each commander array…
ManicMonkOnMac
  • 1,476
  • 13
  • 21
0
votes
3 answers

Reading a file line by line using for..in

I'm trying to to read lines one by one from an external txt file (about 180kb) using Objective-C. I have found this piece of code here on SO. The way I understand it this code will put every line in an element right? - (IBAction)start:(id)sender;…
0
votes
3 answers

How to do fast enumeration to populate an NSDictionary using two NSArray's in Objective-C?

I have two arrays, one that holds key values (myKeys), and the other holds NSString objects(myStrings). I would like to use both arrays to populate a single NSDictionary (myDictionary)using fast enumeration but am not sure how? for (NSNumber *key…
syedfa
  • 2,801
  • 1
  • 41
  • 74
0
votes
1 answer

Fastenumeration implementation

I am trying to implement the countByEnumeratingWithState method in my objective-c class (say MyClass In this method I do an MyOtherClass *cl = [[MyOtherClass alloc] init]; buffer[count++] = cl; The reason why I have to allocate objects on the fly…
Paul Praet
  • 1,367
  • 14
  • 25
0
votes
1 answer

Application not entering Fast Enumeration loop

after much debugging, I have determined that this code is ignoring the fast enumeration loop and blindly jumping to the end: -(void)loadOutAnnotations { NSLog(@"entering Annotation enumeration Loop"); iProspectFresno_LiteAppDelegate…
kevin Mendoza
  • 1,211
  • 2
  • 12
  • 16
0
votes
1 answer

How to sub-total values in a NSMutableArray of NSMutableDictionary's

I know this is more of an how-to but for some reason, I'm over complicating what should be a simple fast enumeration. I simply want to subtotal "Shift Length" for each "Number". I have a NSObject that I created to convert mm:ss to seconds to…
1 2 3
8 9