Re: Implementing NSFastEnumeration
Christiaan Hofman <[email protected]>
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Oct 1, 2009, at 18:43, Gerd Knops wrote: > > On Oct 1, 2009, at 11:30 AM, Christiaan Hofman wrote: >> > [..] > >> While this works for small arrays, for larger arrays this gives a >> memory access crasher. The reason seems to be that NSArray can >> return more items than len (the size of stackbuf) in a single call. >> > Correct. The trick is to use a variable in the state to note you > count. state->state happens to be 0 when first called, and OS X will > not touch it. So you can use it to remember where you were at for > the next call. See my example code. Well but that means that /I/ have to do the enumeration, while my question was really whether I could let NSArray do it. In other words, in my sample I first use NSFastEnumeration on the keys array. When I modify state->state myself I'm pretty sure that I cannot safely do that, because NSArray would want to use that field for its own enumeration. > >> And another question I have been unable to find an answer to in the >> docs. Foundation collections are implemented as class clusters. So >> is NSFastEnumeration implemented as a primitive or a derived >> method? In other words: when I subclass NSArray (or another >> collection class), does it already have a default implementation of >> NSFastEnumeration, or am I forced to always implement it when I >> want to use for(...in...)? >> > AFAIK subclassing class clusters doesn't work, which is why I think > they are a poor design. > > Gerd > That's definitely wrong: subclassing class clusters does work, and I've successfully done it many times. When done right it can actually be a pretty useful design, and apart from that it's important for toll- free bridging. The only thing to know is which methods are primitive, because you have to implement those. And that was exactly my question. Christiaan