Re: Collection classes
Re: Collection classes
- Subject: Re: Collection classes
- From: Marco Scheurer <email@hidden>
- Date: Thu, 8 Jan 2004 20:40:05 +0100
On Jan 8, 2004, at 4:22 PM, j o a r wrote:
@implementation NSMutableArray (MyArrayAdditions)
- (void) addObjectIfMissing:(id) obj
{
if ((obj != nil) && ![self containsObject: obj])
{
[self addObject: obj];
}
}
@end
And voila, an ordered collection of unique items. No need for a
separate class for that purpose.
Where insertions are o(n), which could be problematic if you think of
it as a replacement for the o(1) addObject: .
You also do not really have an ordered uniqued collection, but the
possibility of using an NSMutableArray as one. The addObject: and
insertObject:atIndex: are still there. (In practice and with discipline
that's OK: there's no Stack class in Foundation either, all you have to
do is use revealing names for your instance and access methods.)
I tend to agree with Timothy that some abstractions are missing from
the Foundation classes. For instance I've often been bothered by the
lack of an abstract Collection class or protocol in EOF, where all the
APIs are based on arrays while what you often deal with are sets.
However, things also rapidly break down because of single inheritance
and the Collection/MutableCollection choice. How do you organize
NSCollection, NSMutableCollection, NSArray and NSMutableArray ?
As always this is a trade off and in practice Foundation's small
collection of collection classes works pretty well. I'm not missing
anything from Java, but perhaps a few from Smalltalk.
marco
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.