Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
- Subject: Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
- From: JongAm Park <email@hidden>
- Date: Thu, 30 Jun 2011 14:46:27 -0700
Wow.. great information!
Thank you very much for sharing your idea!
It definitely helped me!
JongAm Park
On Jun 30, 2011, at 2:41 PM, Quincey Morris wrote:
>
> On Jun 30, 2011, at 13:51, JongAm Park wrote:
>
>> The rationale behind "enumerator" pattern is to unify the way to access collection classes no matter what they actually look like.
>> So, enumerator pattern is actually written in index based iteration wrapped with enumerator pattern.
>> Similarly, I guessed fast enumeration was based on either index iteration or enumerator pattern.
>
> No, not necessarily, if by "index iteration" you mean 'objectAtIndex:'.
>
> NSArray has 2 primitive methods (count and objectAtIndex:) that a concrete subclass must implement. It also conforms to NSFastEnumeration, so a concrete subclass must also implement 'countByEnumeratingWithState:objects:count:'. That's three primitive methods you know for sure are implemented in any concrete subclass.
>
> There's no way of knowing (in general) whether these primitive implementations make use of each other. I'm virtually certain, for example, that in NSCFArray (the standard but private concrete subclass of NSArray), countByEnumeratingWithState:objects:count: doesn't use objectAtIndex:, because part of the point of fast enumeration is to eliminate per-object method calls if possible. I'm also virtually certain that NSCFArray's enumerator uses the fast enumeration method countByEnumeratingWithState:objects:count: directly, rather than using objectAtIndex:.
>
> The method you wrote is non-primitive. However, you know that all of the primitive methods and protocols are implemented, so it's safe to use those directly (as others already replied). It's also safe to use all of the standard non-primitive methods, because the abstract NSArray class provides default implementations of all of them, regardless of whether a subclass overrides them for performance reasons.
>
>> Also, fast enumeration is a language feature. So, if Objective-C without fast enumerator is used, methods written with fast enumerator would not work.
>
> If an older Objective-C runtime is used, you'll get an "invalid selector" exception for 'countByEnumeratingWithState:objects:count:', so yes it would not work in that sense.
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden