Re: Categories overriding methods
Re: Categories overriding methods
- Subject: Re: Categories overriding methods
- From: "John C. Randolph" <email@hidden>
- Date: Fri, 2 Aug 2002 01:04:07 -0700
David,
When the runtime loads a category, the methods in that category are
inserted into the class's method table. If a method in a category has
the same name as a method that's already there, the one being loaded
will replace the earlier method. Sometimes, this is a good thing since
it allows for replacing (patching out) a buggy original method, but
keep in mind that the method in the category has no way to refer to the
method it clobbers. Categories are *not* like subclassing.
BTW, it's usually more fruitful to describe what you're trying to do
and why you want to do it, than to ask the list about a low-level
detail of runtime behaviour like this.
This is some pretty tricky stuff you're trying, and it sounds to me
like what you really want is for your subclass to pose as its parent
class.
See [NSObject +poseAsClass:]
-jcr
On Thursday, August 1, 2002, at 09:06 PM, David Newberry wrote:
Hello all,
I am thinking about using a category to over-ride a class's init:
method to return a subclass of that class. (Make sense? :) My question
is basically, is that OK? I'm getting a mixed impression from the >
docs:
"It's best if categories don't attempt to redefine methods that are
explicitly declared in the class's @interface section. ...
Note: When a category overrides an inherited method, the new version
can, as usual, incorporate the inherited version through a message to
super. But there's no way for a category method to incorporate a
method with the same name defined for the same class."
I read that and basically... I don't get it. This is what I'm thinking
of trying to do...
In my category (of MyClass):
- (id)initWithInfo:(id)info
{
[self autorelease];
return [[MySubclass alloc] initWithInfo:info];
}
In MySubclass:
- (id)initWithInfo:(id)info
{
return [super initWithInfo:info]; // calls MyClass' initWithInfo:
}
Is this... reasonable code? The reason I want to do this is because I
want code that's already wired to create instances of MyClass to
instead create instances of MySubclass without fiddling with the
existing code.
Thanks,
-David Newberry
_______________________________________________
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.
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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.