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

Fast Enumeration of a NSFetchedResult

So I have a NSFetchedResultsController. I have it working fine for normally displaying the data. I have a situation where I need to be able to enumarate through them. So I fetch the results as seen here: if (![[self fetchedResultsController]…
logixologist
  • 3,694
  • 4
  • 28
  • 46
1
vote
4 answers

Objective-C: Count to every "tenth" in array

I was wondering how do I get sort through an do something different based on every "tenth" item in an array. I don't know much but I think it would kind of go like this... for (NSDictionary *object in array) { if (0==(object % 10)) { …
Brandon A
  • 8,153
  • 3
  • 42
  • 77
1
vote
1 answer

What happens when you declare the pointer outside of the fast enumeration loop in objective c?

I have seen it done both ways but I am not really sure what the difference is. Here is both scenarios: Outside of fast enumeration loop: NSDate *date; for(date in array) { // } Inside of fast enumeration loop: for(NSDate *date in array) { …
user3451821
  • 123
  • 7
1
vote
3 answers

Iterating through blocks vs fast enumeration vs for loop

I thought iterating through Blocks is faster than enumeration and in some case it does. However with this simple example where I have a data array and I am creating multiple arrays using different iterating approaches and the results are not…
1
vote
1 answer

NSGenericException', reason: '*** Collection <__NSArrayM: 0x12a9f7d0> was mutated while being enumerated in MAPS

My stacktrace is related to maps.. NSGenericException occurs, if we try modify the array, which is being enumerated... I have taken-care about not modifying the enumerating Array. for (int k=0;k<[[af factsArray] count];k++) //here af is my…
1
vote
1 answer

fast enumeration versus enumerateObjectsUsingBlock

Consider: - (void) testing { NSMutableArray * numbers = [NSMutableArray arrayWithCapacity:10000000] ; for (int i = 0 ; i < 10000000 ; ++i) { [numbers addObject:@(i)] ; } NSInteger (^sum)(NSInteger, NSInteger) = ^(NSInteger…
verec
  • 5,224
  • 5
  • 33
  • 40
1
vote
1 answer

Search in Array (from plist with dictionaries) with Fast enumeration or NSPredicate

My app reads data from a plist file and displaying the data in a UITableView my plist EXAMPLE: name One value 1 name
Jonathan Gurebo
  • 1,089
  • 1
  • 11
  • 21
1
vote
2 answers

Early Exit from Fast Enumerate Loop?

When using fast enumeration, is there a way to exit early, i.e. before going through every element in the array? for (element in myArray) { //is there a way to exit before running through every element in myArray? }
VikR
  • 4,818
  • 8
  • 51
  • 96
1
vote
1 answer

What's the fastest way to hash a very large dataset for UICollectionView Layout...NSIndexPath is too slow

I have a UICollectionViewController with a large dataset (>2000 items) with a custom layout. Using sections, the scrolling performance became extremely choppy. Using Instruments and a few tests, I determined this was due to lookup in the layout…
1
vote
1 answer

proper value to type in the fast enumeration

i have a model (core data) set up, called Animals. i have to access this in a specific order and to do so i use the code listed below. what am i supposed to type instead of the XXXXXXXXX in the for loop? NSSortDescriptor *sort = [NSSortDescriptor…
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
1
vote
2 answers

Trying to dynamically add UILabels to a view by doing fast enumeration of an NSMutableArray

I have an NSMutableArray that contains objects of type Person. The Person object contains parameters of NSString *name, NSString *dateStamp, and NSString *testScore. What I would like to do using fast enumeration, is display the parameters of each…
syedfa
  • 2,801
  • 1
  • 41
  • 74
1
vote
1 answer

How to check to see if an object exists in NSMutableArray without knowing the index, and replace it if it exists, in iOS?

I have an NSMutableArray that contains objects of type Person. The Person object contains parameters of NSString *name, NSString *dateStamp, and NSString *testScore. What I would like to do using fast enumeration, is to check to see in the…
syedfa
  • 2,801
  • 1
  • 41
  • 74
1
vote
3 answers

NSMutableArray fast enumeration issue

Newbie obj-c question. I have a custom tableview with four custom cells. In every cell is a editable textfield for customer info. I need to improve switching between textfields by Input Accessory View. http://uaimage.com/image/62f08045 I created an…
Olex
  • 1,656
  • 3
  • 20
  • 38
1
vote
1 answer

'Assigning to 'id' from incompatible type'

I'm implementing a objective C wrapper for Box2d (which is written in c++). The b2Body keeps a reference to its wrapper B2Body in its userData field. GetUserData returns a void*. I'm now implementing fast iteration for getting the B2Bodies out of…
Fredrik Johansson
  • 1,301
  • 1
  • 13
  • 26
1
vote
2 answers

How to fast enumerate and compare objects of many NSArrays in Objective-C?

I have many different NSArray's stored in .dat files, in the Documents folder of my iPhone application, like so: john.dat mary.dat bob.dat etc... The number of .dat files is unknown and it will increase or decrease according to a number of factors…
neowinston
  • 7,584
  • 10
  • 52
  • 83
1 2 3
8 9