Search code examples
nswindowobjective-c++

Warning: XXX may not respond to YYY


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.


Solution

  • 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.