Let’s say I have this class and its subclass
@interface MySuperClass
- (void)open:(id)type value:(id)value;
- (void)openWebpage:(NSURL*)url;
@end
@interface MySubClass
- (void)openWebpage:(MyBookmarkClass*)bookmark;
@end
and that calling [someMySubclass openWebpage:someBookmark] calls [super open:BookmarkClass value:self.url]. And open:value calls [self openWebpage:url].
I realize this is very contrived, but I ran into a similar situation. My confusion is that even though [self openWebpage:url] is being called in MySuperClass, when it gets executed openWebpage: is being run in the context of the caller, MySubClass, which doesn’t know what to do with an NSURL.
So my question is: is there any way to force something to be called in its original context? Or make it as though it calls super as many time as it can up the chain and find the method closest to the top?
There is only one context. There’s a single object. Its class is
MySubClass.It is a mistake to have overridden the method with a different incompatible type. Don’t do that. This is not C++ with function overloading. There’s no dispatch based on the type of arguments.
The convention is to name methods by what they’re acting on. So, you may have a method named
-openWebpageURL:inMySuperClassand another method introduced inMySubClassnamed-openWebpageBookmark:. Note thatMySubClasswould still have a method named-openWebpageURL:inherited fromMySuperClass.