Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
Dave DeLong wrote: You can just do: NSArray *objectsArray = [theArray valueForKey:key]; And it'll do pretty much the same thing (except that it'll call -valueForKey: on each item in the array, and not objectForKey:. However, if the objects are NSDictionaries, that's pretty much the same thing). Pretty much. But just this week I ran across a situation where sending objectForKey: worked while valueForKey: said the dictionary wasn't KVC-compliant for the specified key. ___ 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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
On 1 Jul 2011, at 12:59, Gregory Weston wrote: Dave DeLong wrote: You can just do: NSArray *objectsArray = [theArray valueForKey:key]; And it'll do pretty much the same thing (except that it'll call -valueForKey: on each item in the array, and not objectForKey:. However, if the objects are NSDictionaries, that's pretty much the same thing). Pretty much. But just this week I ran across a situation where sending objectForKey: worked while valueForKey: said the dictionary wasn't KVC-compliant for the specified key. I believe this would be when the key begins with an @ symbol. ___ 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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
Yeah, that should be fine, but it's unnecessary. You can just do: NSArray *objectsArray = [theArray valueForKey:key]; And it'll do pretty much the same thing (except that it'll call -valueForKey: on each item in the array, and not objectForKey:. However, if the objects are NSDictionaries, that's pretty much the same thing). Dave On Jun 30, 2011, at 11:59 AM, JongAm Park wrote: Hello, I wrote a method for NSArray. - (NSArray *)objectsForKey:(id)key { NSMutableArray *objectsArray = [NSMutableArray arrayWithCapacity:10]; for( id item in self ) { [objectsArray addObject:[item objectForKey:key]]; } return [[objectsArray copy] autorelease]; } What I'm curious is if it is OK to use fast enumeration to implement an NSArray method itself. Because the mechanism for fast enumeration is already there, I basically think it will be OK. However, wouldn't it better to rely on the most fundamental mechanism like objectAtIndex:? I found someone's implementation here at http://svn.opengroupware.org/SOGo/inverse/trunk/SoObjects/SOGo/NSArray+Utilities.m Approaching such a way looks reasonable to me, because it should work whatever underlying mechanism is supported by Objective-C. Will there be any good/bad aspect in using fast enumeration in this case? Thank you. ___ 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/davedelong%40me.com This email sent to davedel...@me.com ___ 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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
You might also try HBCollections, a series of collections categories I wrote that makes it easy to do stuff like this. https://github.com/hborders/HBCollections -Heath On Jun 30, 2011 2:10 PM, Dave DeLong davedel...@me.com wrote: ___ 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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
Um... Thanks for your reply. The last time I used that was about 5 or 6 years ago, and I wondered yesterday where it went away. :) Thanks for pointing out that method. BTW, my actual question was if it is meant to use fast enumeration in a collection class implementation. Although I tried to add objectsForKey: to existing NSArray, it should be OK, because it is to rely on existing, fully functional one. However, let's say we write a class like NSArray that supports fast enumeration, block based enumeration, numerator based enumeration and finally index based iteration. 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. Also, fast enumeration is a language feature. So, if Objective-C without fast enumerator is used, methods written with fast enumerator would not work. So, my question was originally for that. :) Anyway, it's not big deal now. Thank you :) JongAm Park On Jun 30, 2011, at 12:02 PM, Dave DeLong wrote: Yeah, that should be fine, but it's unnecessary. You can just do: NSArray *objectsArray = [theArray valueForKey:key]; And it'll do pretty much the same thing (except that it'll call -valueForKey: on each item in the array, and not objectForKey:. However, if the objects are NSDictionaries, that's pretty much the same thing). Dave ___ 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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
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 (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
Re: [Q] Will the be any problem in implementing an NSArray method using fast enumeration?
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 (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