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?
How to implement the NSFastEnumeration protocol?
7.3k views Asked by TheAmateurProgrammer At
2
There are 2 answers
0
On
Just reviving this thread after finding an excellent explanation. The Apple link seems to be broken. You can try here: https://developer.apple.com/library/ios/#samplecode/FastEnumerationSample/Introduction/Intro.html
The best example for implementing fast enumeration that I've found is at: http://mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html. It looks much worse than it is.
Apple's FastEnumerationSample shows you what to do, but here's a breakdown.
The sole
NSFastEnumeration
method,countByEnumeratingWithState:objects:count:
, returns chunks of the collection. It's executed whenever more items are needed, until it indicates that there are no more items by returning 0. A chunk is passed as a C array ofid
s.Within the method, the
state
parameter holds most (if not all) of the data you'll be using. You'll need to setstate->itemsPtr
and updatestate->state
with each separate invocation ofcountByEnumeratingWithState:objects:count:
. Here's a brief description of each field ofNSFastEnumerationState
:state
: represents the position in the sequence being iterated over. For indexed collections, this would be the index. For linked lists, this could be a node pointer. For other types, this could be a more complex type (e.g. for a tree,state->state
could be an NSMutableArray used as a stack to store nodes). WhencountByEnumeratingWithState:objects:count:
is first called,state->state
is 0; check for this condition to initialize thestate
struct.itemsPtr
: the items in the chunk; points to a C array ofid
s. Cocoa will loop over this array, binding each item in turn to the variable named in the for-in loop.mutationsPtr
: for mutable collections, used to indicate that the collection has changed since the last call tocountByEnumeratingWithState:objects:count:
. Typically, you'd set this once when initializing the state. Collection mutators increment the value that this points to. Cocoa will compare the value returned bycountByEnumeratingWithState:objects:count:
to the value from the previous invocation; if they're different, Cocoa will throw an exception.extra
: you can use this to store extra data.You can set
state->state
and any element ofstate->extra
to whatever you wish; they're provided solely for your convenience, and do not affect Cocoa.state->itemsPtr
,*state->mutationsPtr
and the value returned by the method, however, do affect Cocoa.As for the two other method parameters,
stackbuf
is an array that Cocoa provides to hold items. Its use is optional, but if you don't use it, you'll have to allocate storage space forstate->itemPtr
. If you use it, setstate->itemsPtr
tostackbuf
with each invocation.len
is the length ofstackbuf
, the maximum number of items that you'll be able to store in it.Further reading: