Protocol or class specific selector in objective-c

158 views Asked by At

Using a selector to call a method that exists in more that one class (but with different signatures on return or argument types) causes an Multiple methods named [method name] found... error.

This has been already settled in other questions:

A related problem happens if the repeated method is in a protocol. Having a strongly typed protocol object is still ambiguous for the compiler since the object could also be an instance of other classes implementing the same-ish signature method:

@protocol ExistingMethodProtocol <NSObject>
// This method also exists in UIView, but with a different return type
- (NSString*)contentMode;
@end

@interface ImplementingClass : NSObject <ExistingMethodProtocol>
@end

@implementation ImplementingClass
- (NSString*)contentMode {return nil;}

- (void)problematicCall
{
    // Multiple methods named... error
    [self performSelector:@selector(contentMode)];

    id<ExistingMethodProtocol> idOfProtocol = self;
    // Multiple methods named... error too, even casted
    [idOfProtocol performSelector:@selector(contentMode)];
}
@end

An alternative is to create the selector separately and then perform, thus bypasing the compiler check but causing a warning:

SEL selector = @selector(contentMode);
// Warning: PerformSelector may cause a leak because its selector is unknown
[object performSelector:selector];

What other alternatives could work here for checking and performing a method when a protocol has a method colliding with a same-ish signature?

1

There are 1 answers

0
lead_the_zeppelin On

my 2 cents. Use Runtime reference. In which case it bypasses the compiler warnings/error. Looks like there are several ways to do this, here is my solution.

#import <objc/message.h>

struct objc_method *method = class_getInstanceMethod([self class], @selector(contentMode));
id answer = method_invoke(self, method);

This should prevent errors from selectors in different protocols named the same. But I am a little unsure how this behaves with methods with different implementations in base and super classes.