警告:XXX 可能无法响应 YYY

发布于 2024-10-09 07:24:50 字数 1132 浏览 0 评论 0原文

嘿,我正在用 Objective-C++ 做一些东西......我必须说,当涉及到 Objective-C 部分时,我完全是个新手......我真的不想学习它,我只是需要它用于访问一些 Mac API(ObjC 是一种愚蠢的语言)。

所以 - 用 g++ -x Objective-c++ 编译 - 我不知何故不断收到这个警告:

XXX may not respond to YYY

首先它是使用 NSScreen,现在它是使用 NSWindow:

NSWindow may not respond to +initWithContentRect:styleMask:backing:defer:

我在某个地方看到我应该将其转换为 id,但没有工作,抛出绝对神秘的错误...

那么 - 这个警告实际上意味着什么以及我应该如何让它停止?

编辑:好吧,显然我需要先分配一个实例,然后我可以调用它的 init 函数...无论如何,现在 GCC 正在报告:

confused by earlier errors, bailing out

没有别的。这是它报告的唯一错误。我认为我的代码中有一些错误没有被报告...所以我将发布问题所在的整个文件:

ONXeWindow::ONXeWindow(int px, int py, int sw, int sh, bool resizable){
    NSRect wr = NSMakeRect(px, py, sw, sh);

    int wf = 1; // titled
    wf += 2; // closable
    wf += 4; // miniaturizable
    wf += (resizable ? 8 : 0); // resizable
    wf += (false ? 128 : 0); // metal bg

    useWindow = [[NSWindow alloc] initWithContentRect:wr styleMask:wf backing:2 defer:YES];
}

另外,是的,框架 AppKit 已导入(在头文件中) - 我不是会让你对我奇怪的文件方案感到困惑。

Hey, I am making some stuff in Objective-C++... And I must say that I am a total newbie when it comes to the Objective-C part... I don't really want to learn it, I kinda just need it for accessing a few Mac APIs (ObjC is such a dumb language).

So - compiling with g++ -x objective-c++ - and I somehow keep getting this warning:

XXX may not respond to YYY

First it was with a NSScreen, now it is with a NSWindow:

NSWindow may not respond to +initWithContentRect:styleMask:backing:defer:

I saw somewhere that I should cast it to id, but didn't work, throwing absolutely cryptic errors...

So - WHAT does this warning actually mean and HOW am I supposed to make it stop?

EDIT: Okay, apparently I need to ALLOCATE an instance first, then I can call its init function... Anyways, now the GCC is reporting:

confused by earlier errors, bailing out

And NOTHING else. This is the ONLY error that it reports. I figured that there is some error in my code that doesn't get reported... So I will post the whole file where the problem is here:

ONXeWindow::ONXeWindow(int px, int py, int sw, int sh, bool resizable){
    NSRect wr = NSMakeRect(px, py, sw, sh);

    int wf = 1; // titled
    wf += 2; // closable
    wf += 4; // miniaturizable
    wf += (resizable ? 8 : 0); // resizable
    wf += (false ? 128 : 0); // metal bg

    useWindow = [[NSWindow alloc] initWithContentRect:wr styleMask:wf backing:2 defer:YES];
}

Also, YES, framework AppKit was imported (in the header file) - I am not going to confuse you with my weird file scheme here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最舍不得你 2024-10-16 07:24:50

该消息并不真正神秘,只是你不懂这种语言(而且你自己也承认,你也不在乎)。

由于 Objective-C 方法是在运行时动态调度的,因此您可以调用编译器不知道的方法,但是编译器会警告您正在这样做。方法名称开头的 + 表示您正在调用类方法(- 表示您正在调用实例上的方法)。由于 NSWindow 没有名为 initWithContentRect:styleMask:backing:defer: 的类方法,因此编译器会向您发出警告,在本例中,这是一个非常好的警告。

您可能会写类似的内容:

NSWindow* myWindow = [NSWindow initWithContentRect:rect styleMask:0 backing:0 defer:NO];

当您打算写类似的内容时:

NSWindow* myWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:0 backing:0 defer:NO];

第一个将消息直接发送到类,但这是一个实例方法。您需要先分配一个 NSWindow 实例,然后发送 init 消息。此外,clang 往往会比 gcc 提供更好的警告和错误消息。 Clang 2.0 还可以很好地处理 C++ 和 ObjC++,因此切换到 clang 可能是值得的。

The message isn't really cryptic, you just don't know the language (and don't care to, by your own admission).

Since Objective-C methods are dispatched dynamically at run-time, you can call a method that the compiler doesn't have any knowledge of, however, the compiler is warning you that you're doing so. The + in the beginning of the method name means that you're calling a class method (a - would indicate that you're calling a method on an instance). Since NSWindow has no class method named initWithContentRect:styleMask:backing:defer:, the compiler is giving you a warning, and in this case, it's a pretty good one.

You probably wrote something like:

NSWindow* myWindow = [NSWindow initWithContentRect:rect styleMask:0 backing:0 defer:NO];

when you meant to write something like:

NSWindow* myWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:0 backing:0 defer:NO];

The first one sends the message directly to the class, but this is an instance method. You need to allocate an instance of NSWindow first, then send the init message. Also, clang tends to give much better warning and error messages than gcc. Clang 2.0 also handles C++ and ObjC++ pretty well, so it might be worth it to switch to clang.

毁梦 2024-10-16 07:24:50

看看这个示例,看起来像你没有分配你的对象。

Checkout this example, looks like you are not allocating your objects.

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