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
1 answer

How to increment a 'score' during fast enumeration

I have the following code which I would like to use to check user answers and output a score (out of 5). I use a plist with the answers in and check the textField.text against it. What I'm struggling with is: how to get an output score as a total…
Rob W
  • 591
  • 1
  • 7
  • 22
-1
votes
1 answer

Fast-enumeration logic error checking for compare of UITableViewCell

I have a mutable array (editedServiceParts) with two objects in it; I have a table view with about 30 cells in it. I want to check to see if any of table view cells (cell.textLabel.text) appear in the mutable array; if they do, I want to set the…
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
-1
votes
2 answers

How do I access an NSMutableDictionary's property inside an array using fast enum?

I have a "bookshelf" array that contains four NSMutableDictionary "book" (which contains three properties), my question is how do I access the hasBeenRead flag/property of the "book" dictionary to find out which book has not been read? Book(s): { …
0--
  • 221
  • 1
  • 6
  • 16
-1
votes
1 answer

Fast enumeration situation

I'm currently working with fast enumeration variables, something that seems really simple I can't make it work. I have this code, that it work for (NSDictionary *story in stories){ NSLog(@"%@", [story objectForKey:@"title"]); } This actually…
user3241911
  • 481
  • 6
  • 15
-1
votes
2 answers

Trying to iterate through an Array by checking against keys and objects of an NSDictionary in iOS

I have an NSArray of UISwitches. I have separately an NSDictionary whose keys are NSNumbers, and whose objects are BOOL values in the form of NSString objects. What I would like to do is iterate through the NSArray of UISwitches, check to see if…
syedfa
  • 2,801
  • 1
  • 41
  • 74
-1
votes
1 answer

Need to check NSArray holding multiple data types if the objects are null in iOS

I have an NSArray that I'm using in my iOS application which is holding data of three types: NSDate, NSString, and NSNumber What I would like to do is iterate this NSArray in a for loop to check to see if the objects are null, however, I'm unsure…
syedfa
  • 2,801
  • 1
  • 41
  • 74
-1
votes
1 answer

What is the complexity of this algorithm? I thought it was big O(n) - using only 1 for...in loop

ios project https://github.com/HarrisonJackson/iOS-find-top-4-integers-in-big-list---also-use-blocks-and-delegates The algorithm should solve for the top 4 integers in an unsorted array. Here I generate a list of unsorted NSNumbers and iterate over…
HarrisonJackson
  • 370
  • 1
  • 6
-2
votes
1 answer

How to run the fast enumeration like for loop?

In for loop we can simply define print data between 0 to 5 . Like this how we can define in Fast Enumeration? for(int i=0;i<5;i++) { NSLog(@"for loop"); }
S. Karthik
  • 618
  • 5
  • 16
-2
votes
1 answer

Is it possible to use fast enumeration on a NSMutableDictionary?

I have the following method (it is an instance method of ShoppingCart): - (void) showShoppingCartProducts { for (Product *eachProduct in products) { [eachProduct logName] } } I have another method (it is an instance method of…
Fine Man
  • 455
  • 4
  • 17
-2
votes
2 answers

Fast enumeration and adding objects to NSMutableArray

I am attempting to add objects to NSMutableArray "allItems1" for (PMGWine *w in [[PMGWineStore sharedStore]allItems]) { [allItems1 addObject:w]; NSLog(@"%@", w); } NSLog(@"%d", [allItems1 count]); [[PMGWineStore…
Pete
  • 613
  • 1
  • 6
  • 18
-3
votes
1 answer

Objective-C can you use fast enumeration in place of "for (i = 0; i < X; i++)"

Let's say I've got a basic integer iteration like so: NSInteger rowCount = self.rowCount; for (int i = 0; i < rowCount; i++) { // stuff! } Is there a way to implement this using fast enumeration blocks? I could certainly create an array of…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
-3
votes
1 answer

Will a for in loop introspect the contents of an array

Given an array NSArray *myArray = @[@"hello", @5, @[@6, @7, @8]]; for (NSNumber *myNumber in myArray) { NSLog (@"%@", myNumber); } Would the output of the log message be 5 or 5 6 7 8. ie. Does the for/in loop introspect top level objects in a…
altyus
  • 606
  • 4
  • 13
-4
votes
1 answer

Need to generate a JSON document to upload to server after iterating an NSMutableArray of objects with NSString parameters in iOS

I have an NSMutableArray that holds a collection of objects, with each object having three parameters of type NSString. What I need to do is iterate through this collection, and create a single JSON document with two subsections: one is called…
syedfa
  • 2,801
  • 1
  • 41
  • 74
-8
votes
2 answers

Objective-C Fast Enumeration: checking for BOOL

Scenario = I need to loop through an array and find how many "unread" there are and count how many to display to the user. What I'm Looking For = something like this (this is not my real code) for (NSDictionary *dic in self.objects) { …
Tom Testicool
  • 563
  • 7
  • 26
1 2 3
8
9