Search code examples
cocoacompiler-errorscompiler-warningsllvmsuperclass

Avoid "[superclass] may not respond to [selector]" warning without incurring LLVM's "Cannot cast 'super'" error


I have the following code in an NSView subclass:

- (id)forwardingTargetForSelector:(SEL)aSelector
{
    if ([super respondsToSelector:@selector(forwardingTargetForSelector:)]) {
        // cast to (id) to avoid "may not respond to selector" warning
        return [(id)super forwardingTargetForSelector:aSelector];
    } else {
        [self doesNotRecognizeSelector:aSelector];
        return nil;
    }
}

In the first line, return [(id)super ... casts super to id because under the GCC compiler, this suppressed the warning that the superclass (NSView) may not respond to forwardingTargetForSelector:, as suggested in answers such as this one.

However, when I switch to the LLVM compiler, this results in a "Cannot cast super" error. Is there a correct way to modify my code so that I get neither the warning nor the error under both LLVM and GCC?


Solution

  • Declare the selector in an interface-only category in your implementation file.

    @interface NSView (FastForwarding)
    
    - (id) forwardingTargetForSelector:(SEL)selector;
    
    @end