Re: isa
Re: isa
- Subject: Re: isa
- From: Aram Greenman <email@hidden>
- Date: Sat, 1 Jun 2002 13:53:11 -0700
On Saturday, June 1, 2002, at 01:15 AM, Marcel Weiher wrote:
On Friday, May 31, 2002, at 11:57 Uhr, Aram Greenman wrote:
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.
The problem was accessing overriden functionality of superclasses, and
there is an easy way to get this without any tricks whatsoever, just
good coding practices.
Instead of having a method
-doStuff // class A
{
lots of interesting stuff happens here
}
which you then override with in a subclass with:
-doStuff // class A' subclass of A
{
do my subclasses interesting stuff
}
and then not being able to access this in A''
-doStuff // class A'' subclass of A'
{
how do I get the original A implementation? -> you can't!
}
simply split this out into two methods
-doStuffA // class A
{
lots of interesting stuff happens here
}
-doStuff // class A
{
[self doStuffA];
}
now A' can override to its heart's content, A'' can still easily access
the original A method
-doStuff // in class A''
{
[self doStuffA];
}
Done.
But, you ask, how do I do this when doStuff is defined in a system
class that I don't have source code for? Easy. You just have to do a
bit of lateral thinking. Simply define a new selector, -doMyStuff
and use that instead. For the system class you want to use,
implement -doMyStuff { [self doStuff] } in a category.
My way is easier ;)
I also suggested changing the object's isa temporarily, with the
disclaimer that it might be a bad idea.
It is a very bad idea, especially the temporary swizzling.
Exactly how? That is my suspicion, but I would like to know specifically
why it is a bad idea.
Assuming that none of the above dangers are present, can anyone see
any problem with doing this?
Yes, unnecessary complication and confusion.
You could avoid this by implementing a method like:
- (id)performSelector:(SEL)sel asClass:(Class)cls {
Class wasa = [self class];
id result;
isa = cls;
result = [self performSelector:sel];
isa = wasa;
return result;
}
possibly in a category of NSObject, along with
-performSelector:asClass:withObject:, etc.
That way the isa-swizzling is hidden from the rest of your code. Plus if
you saw a message like
[foo performSelector:@selector(bar) asClass:[Baz class]];
it would be pretty hard to get confused about what your intent is.
Aram
_______________________________________________
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: Marcel Weiher <email@hidden>
- Re: isa
- From: Ondra Cada <email@hidden>
- Re: isa
- From: Nat! <email@hidden>
References: | |
| >Re: isa (From: Marcel Weiher <email@hidden>) |