避免“[超类]可能不会响应[选择器]”警告,而不会引发 LLVM 的“无法转换‘super’”错误

发布于 2024-10-08 01:05:02 字数 821 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-10-15 01:05:02

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

@interface NSView (FastForwarding)

- (id) forwardingTargetForSelector:(SEL)selector;

@end

Declare the selector in an interface-only category in your implementation file.

@interface NSView (FastForwarding)

- (id) forwardingTargetForSelector:(SEL)selector;

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文