Re: Implementing NSFastEnumeration
Christiaan Hofman <[email protected]>
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Oct 2, 2009, at 0:21, Tom Hageman wrote:
> Hi Christiaan,
>
> On Thu, 1 Oct 2009 18:30:15 +0200, Christiaan Hofman wrote:
>>
>> [...] The
>> other thing is that I want to enumerate the underlying array of keys
>> first. I tried something along these lines:
>
> How about _replacing_ the key in itemsPtr[i] with the value as you go
> along? Something like this: (Achtung, Mailware!)
>
> - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)
> state
> {
> NSUInteger i, count = [keys countByEnumeratingWithState:state
> objects:stackbuf count:len];
> for (i = 0; i < count; i++)
> {
> id key = state->itemsPtr[i];
> id value = [values objectForKey:key];
> state->itemsPtr[i] = value;
> //stackbuf[i] = value;
> }
> //state->itemsPtr = stackbuf;
> return count;
> }
>
>> 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.
>>
>> Would there be a way to modify this implementation to work? Or am I
>> forced to get the objects from keys without using fast enumeration
>> (in
>> which case fast enumeration becomes hardly fast)?
>
> HTH,
> Regards,
>
> --Tom.
Well, I was wondering about whether that was safe. For instance, when
the itemsPtr points directly to some internal C array of the keys
array (rather than a copy), wouldn't this possibly modify this
internal C array? In fact, when I tried this I got a crash.
Apart from that, Christopher Kane's comments still apply.
Christiaan