Re: Help in 2 Diamensional Array
Re: Help in 2 Diamensional Array
- Subject: Re: Help in 2 Diamensional Array
- From: William Bumgarner <email@hidden>
- Date: Sat, 1 Apr 2006 09:53:32 -0800
On Apr 1, 2006, at 12:18 AM, Andy Lee wrote:
On Mar 31, 2006, at 11:56 AM, j o a r wrote:
On 31 mar 2006, at 18.32, William Bumgarner wrote:
your own array class, potentially as a subclass of NSArray (or
NSMutableArray).
Is it really such a good idea to tell a newbie to attempt to
create subclasses of a class cluster? ;-)
Even if NSArray were a regular class I personally would use a
wrapper class rather than a subclass, because I don't want that
class's interface to include NSArray baggage that isn't relevant to
my needs. For example, I don't want to have to figure out the
semantics of -arrayByAddingObject: in my new class. It gets
hairier if I subclass NSMutableArray.
Subclassing a class cluster is actually very easy once you learn the
pattern.
With class clusters, you merely have to fulfill the class's core API
contract and be done with it. For NSMutableArray, it is:
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding>
- (unsigned)count;
- (id)objectAtIndex:(unsigned)index;
@end
And:
@interface NSMutableArray : NSArray
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(unsigned)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(unsigned)index;
- (void)replaceObjectAtIndex:(unsigned)index withObject:(id)anObject;
@end
The rest of the implementation on the NSArray and NSMutableArray
classes are implemented entirely in terms of those methods.
Concrete subclasses are free to override any of the rest of the
methods to offer more efficient implementations.
Now, of course, none of this is to say that implementing a subclass
of NS*Array is right for your use. Maybe so, maybe not. In the
context of the 2D array, all of the above methods could be made to do
something semi-reasonable in such a context.
Implementing a subclass of NSObject that offers a 2D array of objects
would be straightforward, as well.
In any case,
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden