Re: Collection classes
Re: Collection classes
- Subject: Re: Collection classes
- From: Michael Latta <email@hidden>
- Date: Thu, 8 Jan 2004 18:29:46 -0800
Another point that is not obvious to those coming from Java and the
like. Objective-C
is NOT type safe in any way. Even methods that declare (NSArray *) for
arguments WILL take
any object reference that responds to the same messages. The run-time
is all (id) not other references!
There is no need for the object to conform to NSArray *. The class
does not need to be a sub-class. It is not a hierarchical type system.
This means you can create your own class that has an NSArray for order,
and an NSSet for uniqueness tests using a hash function.
You can implement the various methods using one or both of the data
structures. This is basically an array with a uniqueness index.
Then you can use this object for any API calls that call for either
type if you implement the proper messages.
Michael
On Jan 8, 2004, at 9:09 AM, Timothy Reaves wrote:
timothy,
I am implementing a table data source. I have a class (Foo) that has
an
instance attribute (bar) of type NSMutableSet that I want to have the
table display. The contract of bar is that it not allow duplicates,
so it
HAS to be a NSSet*, as that is the only collection that enforces that
contract. The Cocoa data sources in Cocoa want collections that are
indexable, and I understand why.
use NSMutableArray and check for containsObject: before you insert.
i rember apple having a private method called
-(BOOL)addObjectIfAbsent:
that was not in the headers of NSMutableArray. so before you add such
a
method as a category, try to just create a header for it. if you can
use it fine (it's still in there somewhere) if you get
selectorNotRecognized just implement it yourself and you can use
NSMutableArray as an indexable list with unique entries.
Well, that would work, and it might be the way I end up going.
But I
do not like it. It the code I was working on were meant to be
reused, even by me, it could be very easy to confuse why I was using
an array instead of a set, when it is clear that a set is called for.
If I wanted to add a class, an NSOrderedSet would be very easy to
create composing an array into it, and wrapping all the mutators with
the check you suggest.
_______________________________________________
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.
_______________________________________________
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.