我在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下既没有警告也没有错误?
答案 0 :(得分:7)
在实现文件的仅接口类别中声明选择器。
@interface NSView (FastForwarding)
- (id) forwardingTargetForSelector:(SEL)selector;
@end