Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of adopting/implementing fast enumeration for my class?

I'm trying to understand how to adopt the fast enumeration protocol (under iOS/objective C) for a class I'm creating. I read the section of Apple's docs, but... I don't quite get it!

Anyone have some sample code I could look at?

What I'm trying to do: I have an array of objects over which I want a sender to iterate. The sender wants to use a for-in construct. The catch is I don't want the sender to see all the objects in the array because some of them aren't valid in the context of the application.

In other words, I want my iterator to return a subset of objects in the array matching certain criteria.

I'd prefer not to create any new arrays in the process so as not to slow things down.

like image 590
Anna Dickinson Avatar asked Jun 07 '12 15:06

Anna Dickinson


2 Answers

Much better explanation here:

http://www.cocoawithlove.com/2008/05/implementing-countbyenumeratingwithstat.html

NB: Apple's source code is technically correct, but poorly explained, and mostly useless unless you enjoy playing "guess what the programmer was thinking / smoking that day".

Liekwise, Mike Ash's post is correct, and useful after you know how to do it, but terrible as a starting point.

like image 20
Adam Avatar answered Sep 28 '22 17:09

Adam


Apple's own FastEnumerationSample demonstrates it quite well. From the sounds of it, you may not have seen it yet.

There's also this blog post.

like image 54
justin Avatar answered Sep 28 '22 17:09

justin