Peter Lapisu
Peter Lapisu

Reputation: 20975

Super class checking for respondsToSelector fails

I did the following code, to check, if super implement the method (using respondsToSelector)

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if ([super respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [super scrollViewWillBeginDragging:scrollView];
    }

    self.scrolling = YES;
}

but it evaluates to YES and than it breaks on unrecognized selector

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[InputFormViewController scrollViewWillBeginDragging:]: unrecognized selector sent to instance 0x17da8670'

Is this the desired (faulty) behavior, or am i doing smth. wrong?

Note: super class is

@interface FormBaseViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 

that doesnt implement (scrollViewWillBeginDragging:)

Upvotes: 0

Views: 253

Answers (1)

Chuck
Chuck

Reputation: 237020

[super respondsToSelector: @selector(someSelector)] doesn't test whether the superclass implements someSelector. It tests whether the current object (i.e. self) responds to someSelector, but it does it using the superclass's implementation of respondsToSelector:. In other words, unless you have overridden respondsToSelector:, the code [super respondsToSelector: @selector(someSelector)] is exactly the same as [self respondsToSelector: @selector(someSelector)], because you were already using the inherited version.

What you want is [[[self class] superclass] instancesRespondToSelector: @selector(someSelector)].

Though I am not sure why you'd do this, since dynamic superclasses are not really supported by the language.

Upvotes: 3

Related Questions