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

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

I've been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I'm having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop: A reference to a Boolean…
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
44
votes
2 answers

Fast enumeration over nil object

What should happen here? Is it safe? NSArray *nullArray=nil; for (id obj in nullArray) { // blah } More specifically, do I have to do this: NSArray *array=[thing methodThatMightReturnNil]; if (array) { for (id obj in array) { // blah …
Nick Moore
  • 15,547
  • 6
  • 61
  • 83
33
votes
2 answers

Does fast enumeration in Objective-C guarantee the order of iteration?

Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this. i.e. is for (id val in array) { NSLog(@"%@", val); } always going to print out the same as for (int i = 0; i < [array count];…
31
votes
2 answers

Loop through subview to check for empty UITextField - Swift

I"m wondering how to essentially transform the objective c code below into swift. This will loop through all the subviews on my desired view, check if they are textfields, and then check if they are empty of not. for (UIView *view in…
Fudgey
  • 3,793
  • 7
  • 32
  • 53
28
votes
6 answers

Keep track of index in fast enumeration

I want to get the index of the current object when using fast enumeration, i.e. for (MyClass *entry in savedArray) { // What is the index of |entry| in |savedArray|? }
Shri
  • 2,129
  • 2
  • 22
  • 32
24
votes
10 answers

Fastest way to check if an array contains the same objects of another array

The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual: as they are differently sorted. I already tried the solution…
FrankZp
  • 2,022
  • 3
  • 28
  • 41
23
votes
2 answers

How to implement the NSFastEnumeration protocol?

I have a class and I want my class to confirm to the NSFastEnumeration Protocol. I've read the documentation but it's not really clear. Can someone please tell me what the protocol method should return and how it works?
TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
13
votes
3 answers

With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

I'm communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the front of the NSDictionary. When I fast enumerate…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
11
votes
3 answers

Fast Enumeration Vs NSEnumerator in Objective-C

I have seen this over and over, why exactly is it faster to use fast enumeration in loops rather than an NSEnumerator using nextObject:.
shreyasva
  • 13,126
  • 25
  • 78
  • 101
11
votes
4 answers

Objective c "for each" (fast enumeration) -- evaluation of collection?

It seems from experimentation that the collection expression is evaluated only once. Consider this example: static NSArray *a; - (NSArray *)fcn { if (a == nil) a = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; …
Marc Rochkind
  • 3,678
  • 3
  • 30
  • 38
8
votes
2 answers

Why is NSFastEnumeration fast?

Does anybody know whether NSFastEnumeration is really faster (as in run-time performance) than using NSEnumerator or (for arrays) using an integer counter and loop through the elements? If it is indeed faster, how does it achieve that speed? Or…
adib
  • 8,285
  • 6
  • 52
  • 91
8
votes
1 answer

Core Data/NSOperation: crash while enumerating through and deleting objects

I have a core data based app that has a one object (a list) to many objects (list items) relationship. I'm working on syncing data between devices, and as part of that I import lists from XML files in background threads (via an NSOperation…
Jim Rhoades
  • 3,310
  • 3
  • 34
  • 50
7
votes
4 answers

swift fast enumeration of optionals

Is there are better way to do this? Something that looks nicer syntax wise? let a : [Any] = [5,"a",6] for item in a { if let assumedItem = item as? Int { print(assumedItem) } } Something like this, but then with the correct syntax? …
user965972
  • 2,489
  • 2
  • 23
  • 39
6
votes
5 answers

Objective C: Last object when using Fast Enumeration?

What is the best way to know when I have reached the last object in an array when using fast enumeration? Is there a better way than incrementing an int and then comparing that to the length of the array?
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
6
votes
1 answer

Fast Enumeration through UICollectionView Cells - Swift

I am trying to fast enumerate through all of my collection view cells, however this implementation below is giving me a warning. for cell in self.collectionView?.visibleCells() as [UICollectionViewCell] { // Do Stuff } Error below appears on…
Fudgey
  • 3,793
  • 7
  • 32
  • 53
1
2 3
8 9