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
1
vote
3 answers

Do we release an argument in fast enumeration

do we release an argument in fast enumeration? Therefore would this code be accurate: for (MKCircle *circle in localOverlays) { [mapView addOverlay: circle]; [circle release]; // Is it perfectly alright to call this? } I am just…
MCKapur
  • 9,127
  • 9
  • 58
  • 101
1
vote
1 answer

How to avoid copy and pasting?

I'd like to improve this method if possible: this is a small section whereby all of the textfield (eyepiece, objectivelenses etc) texts are saved. Unfortunately, having to do this lots of times for each part of my app is prone to error so I would…
Rob W
  • 591
  • 1
  • 7
  • 22
1
vote
1 answer

compare new CGPoint value to a CGPoint value in Array

I'm generating a number of circles with random positions. what I want to prevent is the circles from overlapping each other. so I want to compare the newly generated CGPoint value to the ones that are in the array and have it generate a new value if…
Ramin Afshar
  • 989
  • 2
  • 18
  • 34
0
votes
2 answers

Objective C fast enumeration trouble

I am trying to use fast enumeration to print all songs that are in a playlist, but it seems like I am doing it wrong. Could someone help me out? I have defined a Song class like this : @interface Song : NSObject @property (nonatomic,strong)…
nemesis
  • 1,349
  • 1
  • 15
  • 30
0
votes
1 answer

Is this piece of code going to crash my app at random times?

for(UIView *view in [_scrollView subviews]) { NSMutableArray *_mutableArray = [_array mutableCopy]; [_mutableArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"guid like %@",view.object.guid]]; if([_mutableArray count] != 0)…
Horatiu Paraschiv
  • 1,750
  • 2
  • 15
  • 37
0
votes
1 answer

Reading Array within NSDictionary into UITableViewCell Subviews

I am attempting to read the plist dictionary below into cells of a UITableView. I want to create a subview for each of the strings contained in the array denoted by the ArrayKey like: Row1: | myString1 | Row2: | myString2 || myString3 | I've used…
0
votes
1 answer

What are the restrictions on fast enumeration?

Please correct me if I am wrong. If we use fast enumeration: NSMutableDictionary We can't add/remove new entries, but we could change existing entries. NSMutableArray We can't add/remove new entries, but we could change existing…
Voloda2
  • 12,359
  • 18
  • 80
  • 130
0
votes
1 answer

Efficiently random enumeration of files from huge directory

I want to be able to enumerate files with a specific search pattern (e.g., *.txt) recursively from a directory. But with couple of constraints: The mechanism should be very efficient. The goal is to enumerate file one by one (using IEnumerable), so…
0
votes
2 answers

If the type of objects in a collection is known, should I specify the type of the iterating variable when using fast enumeration?

For example, say I have an NSArray that contains a bunch of NSString objects: NSArray *games = [NSArray arrayWithObjects:@"Space Invaders", @"Dig Dug", @"Galaga", nil]; What are the pros and cons of doing this: for (id object in games) { …
sys 64738
  • 197
  • 9
0
votes
1 answer

What is the space complexity of iterating over keys in NSMutableDictionary?

Trying to determine run-time and space complexity of a HashTable (NSMutableDictionary) based solution following question: Implement a TwoSum interface that has 2 methods: Store and Test. Store adds an integer to an internal data store and Test…
0
votes
0 answers

Cant use Fast enumeration to fill sqlite table

I have a messages object called masterMessages. That looks like this: self.messages = ( " {\n messageBody = Jj;\n recipientId = XvvxETqjph;\n senderId = XvvxETqjph;\n …
ian
  • 1,002
  • 2
  • 12
  • 29
0
votes
0 answers

Memory Leak with ARC for fast enumeration

On a background thread checking for intersections I was leaking a large amount of memory. I tracked down where the leak was occurring and it was due to fast enumeration. I tried using an @autorelease but that did not fix it either. What ended up…
0
votes
1 answer

Is Fast Enumeration messing with my text output?

Here I am iterating through an array of NSDictionary objects (inside the parsed JSON response of the EXCELLENT MapQuest directions API). I want to build up an HTML string to put into a UIWebView. My code says: for (NSDictionary *leg in legs ) { …
Dan Ray
  • 21,623
  • 6
  • 63
  • 87
0
votes
1 answer

How to pause a for loop in objective C, Spritekit

I was just wondering, if there was any way of pausing a for loop. For instance, for (i = 0; i<10 ; i++) { NSLog(i); //Pause should go here } The outcome should be: 1 (wait 1 sec) 2 (wait 1 sec) etc. Firstly, I thought that you could add SKAction…
user4103894
0
votes
1 answer

One more "Mutated while being enumerated" issue

I faced "Collection <_NSFaultingMutableSet: 0x7fac9c011190> was mutated while being enumerated" issue. I know what it means, I found it on SO. I spotted the line which causes this exception. But I still can't get what exactly I'm mutating here.…
rightaway717
  • 2,631
  • 3
  • 29
  • 43
1 2 3
8 9