Re: isa
Re: isa
- Subject: Re: isa
- From: "John C. Randolph" <email@hidden>
- Date: Fri, 31 May 2002 15:57:35 -0700
On Friday, May 31, 2002, at 02:57 PM, Aram Greenman wrote:
A follow up to my own post:
Previously on this list we were discussing how to get an object
to behave like a class one or more steps up in the inheritance
hierarchy. I suggested two ways to accomplish this.
+instanceMethodForSelector: is one solution, the problem is if
the returned IMP sends messages to self those messages will
still get sent normally and will invoke the implementation of
the object's true class, not the class we want to pose as. This
is the same behavior exhibited by messages to super.
I also suggested changing the object's isa temporarily, with
the disclaimer that it might be a bad idea.
So my question is, is that really a bad idea? Example:
- (void)foo {
Class wasa = [self class]; // remember true isa
isa = [Foo class]; // pretend to be a Foo
[self bar]; // calls -[Foo bar]
isa = wasa; // reset true isa
}
Obvious pitfalls include that the posing class may not respond
to the selector, or that the called method may send self a
message the posing class doesn't respond to, or that the called
method or any methods it calls in turn may attempt to access
instance variables which don't exist in the posing class. The
first case can be checked for at runtime, the latter two can't
but could only occur if the object was posing only as a class
it didn't inherit from.
Assuming that none of the above dangers are present, can anyone
see any problem with doing this? I would really like to get
some opinions on this.
Well, generally speaking, swizzling your isa pointer is a
dangerous thing to do, but one of the benefits of Obj-C being
derived from C is that you can do unsafe things when you want.
The real question here is, what is it you need to do? If your
subclass needs to act like something two levels back in its
inheritance tree, maybe you subclassed from the wrong parent.
-jcr
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.
- Follow-Ups:
- Re: isa
- From: Jeff LaMarche <email@hidden>
References: | |
| >isa (From: Aram Greenman <email@hidden>) |