On 30 Jun 2011, at 22:41, 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.

No, the first two are the primitive methods, and that's it. They are all you 
need to implement. You may choose to implement other methods for performance, 
but they are not essential.

NSArray itself implements first enumeration by calling through to the primitive 
methods automatically for you. (So yes, that's not actually any faster than 
calling -objectAtIndex: yourself repeatedly; it's more the convenience that 
buys you).

NSArray's concrete subclasses that you generally work with — NSCFArray etc. — 
then re-implement fast enumeration to be truly fast and avoid the 
-objectAtIndex: bottleneck.
> 
> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to