Re: self = [super init] debate.
Re: self = [super init] debate.
- Subject: Re: self = [super init] debate.
- From: Bill Bumgarner <email@hidden>
- Date: Sun, 10 Feb 2008 18:28:08 -0800
On Feb 10, 2008, at 5:32 PM, Quincey Morris wrote:
-- Someone did an automated check of every Cocoa class in Tiger
(maybe it was every subclass of NSObject, I don't remember exactly
now) and counted the number of classes for which the receiver of
[super init] was different from the returned value. The answer was 0.
That may comfort anyone who wants to leave out the 'self =', but
it's hardly a reliable argument. The number of dead people reading
this discussion is also 0 (afaik), but I wouldn't take that as proof
of immortality.
It would be impossible to test every conceivable set of arguments.
For example, NSData may return instances of different classes
depending on how large a chunk of memory is desired or if it is a
memory mapped vs. malloc()d chunk o' bits.
Similarly, this is entirely an implementation detail. Any given class
may return a subclass at some future time when it doesn't now. Or it
might even return a different class on a different architecture -- 32
bit vs. 64 bit, for example.
-- Some classes cannot be subclassed -- cluster classes like
NSArray, for example. These classes prove nothing one way or another
about writing subclass initializers.
Class clusters can be subclassed just fine and, while not terribly
common, there are some very good reasons for doing so.
It isn't hard, either, once you grok the pattern. You are required to
implement the methods that are core to the class. All other methods
are implemented on the base of the class cluster in terms of said core
methods. You are, and the system provided class clusters typically
do, override any of the other methods to provide optimized
implementations.
To subclass NSArray, first have a look at the NSArray declaration:
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding>
- (unsigned)count;
- (id)objectAtIndex:(unsigned)index;
@end
This indicates that you need to override the two declared methods and
implement the various protocols. Once done, your NSArray subclass
will interoperate perfectly well with any API that takes an NSArray.
If you want to subclass NSMutableArray, you need to implement
overrides for:
@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
If you fail to implement any one of the methods provided in the
abstract base class of the class cluster, a mighty complaint will be
raised at runtime if said method is executed.
b.bum
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden