避免“[超类]可能不会响应[选择器]”警告,而不会引发 LLVM 的“无法转换‘super’”错误
我在 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:
的警告,如 诸如此类的答案。
但是,当我切换到 LLVM 编译器时,这会导致“无法转换超级”错误。有没有正确的方法来修改我的代码,以便我在 LLVM 和 GCC 下都不会收到警告或错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在实现文件中的仅接口类别中声明选择器。
Declare the selector in an interface-only category in your implementation file.