避免"[超类]可能无法响应[selector]"警告而不会导致LLVM的"无法强制转换'超级'"错误

Isa*_*aac 2 cocoa compiler-errors llvm compiler-warnings superclass

我在NSView子类中有以下代码:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在第一行中,return [(id)super ...转换superid因为在GCC编译器下,这抑制了超类(NSView)可能不响应的警告,如此答案中forwardingTargetForSelector:所建议的那样.

但是,当我切换到LLVM编译器时,这会导致"无法强制转换"错误.是否有正确的方法来修改我的代码,以便在LLVM和GCC下既没有警告也没有错误?

Jen*_*ton 7

在实现文件的仅接口类别中声明选择器.

@interface NSView (FastForwarding)

- (id) forwardingTargetForSelector:(SEL)selector;

@end
Run Code Online (Sandbox Code Playgroud)