Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a method in a super class when it's not exposed

In a subclass, I'm overriding a method that is not exposed in the super class. I know that I have the correct signature as it is successfully overriding the superclass implementation. However, as part of the the new implementation, I need to call the superclass's implementation from the subclass's implementation.

Because it's not exposed I have to invoke the method via a call to performSelector:

SEL superClassSelector = NSSelectorFromString(@"methodToInvoke");
[super performSelector:superClassSelector];

However, in my application this results in an infinite recursive loop where the subclass's implementation is invoked every time I try to invoke the superclass's implementation.

Any thoughts?

I realize this is an atypical situation but unfortunately there's no way to get around what I'm trying to do.

like image 390
Chris Thompson Avatar asked May 21 '13 20:05

Chris Thompson


3 Answers

The way I've dealt with this is to re-declare your super class' interface in your subclass implementation file with the method you want to call from the subclass

@interface MySuperclass()
- (void)superMethodIWantToCall;
@end

@implementation MySubclass


- (void)whateverFunction  {
    //now call super method here
    [super superMethodIWantToCall];
}

@end

I'm not sure if this is the best way to do things but it is simple and works for me!

like image 192
ModernCarpentry Avatar answered Oct 17 '22 06:10

ModernCarpentry


This doesn't work because you're only sending performSelector:, not the selector you pass to that, to the superclass. performSelector: still looks up the method in the current class's method list. Thus, you end up with the same subclass implementation.

The simplest way to do this may be to just write in your own call to objc_msgSendSuper():

// Top level (this struct isn't exposed in the runtime header for some reason)
struct objc_super
{
    id __unsafe_unretained reciever;
    Class __unsafe_unretained superklass;
};

// In the subclass's method
struct objc_super sup = {self, [self superclass]};
objc_msgSendSuper(&sup, _cmd, other, args, go, here);

This can cause problems in the general case, as Rob Napier has pointed out below. I suggested this based on the assumption that the method has no return value.

like image 9
jscs Avatar answered Oct 17 '22 05:10

jscs


One way to go is to create a category of your class in a separate file with the method you are trying to expose

@interface MyClass (ProtectedMethods)

- (void)myMethod;

@end

and on the .m

@implementation MyClass (ProtectedMethods)

- (void)myMethod {

}

@end

Then, import this category from your .m files, and you're good to go. It's not the prettiest thing, but it'll do the trick

like image 2
Ismael Avatar answered Oct 17 '22 06:10

Ismael