2

我在 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 ...转换superid因为在 GCC 编译器下,这抑制了超类 (NSView) 可能无法响应的警告,如this one 等答案forwardingTargetForSelector:中所建议的那样。

但是,当我切换到 LLVM 编译器时,这会导致“Cannot cast super”错误。是否有正确的方法来修改我的代码,以便在 LLVM 和 GCC 下都不会收到警告或错误?

4

1 回答 1

7

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

@interface NSView (FastForwarding)

- (id) forwardingTargetForSelector:(SEL)selector;

@end
于 2010-12-17T20:38:38.390 回答