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

时间:2010-12-17 20:29:54

标签: cocoa compiler-errors compiler-warnings llvm 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;
    }
}

在第一行中,return [(id)super ...super强制转换为id,因为在GCC编译器下,这会抑制超类(NSView)可能无法响应forwardingTargetForSelector:的警告,如answers such as this one中所述。

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

1 个答案:

答案 0 :(得分:7)

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

@interface NSView (FastForwarding)

- (id) forwardingTargetForSelector:(SEL)selector;

@end