Re: Implementing NSFastEnumeration

Tom Hageman <trh-qWit8jRvyhVmR6Xm/[email protected]>
Newsgroups gmane.comp.macosx.devel
Organization Warty Wolfs
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.