"Unrecognized selector sent to instance", even though XCode says the instance is the right type

StackOverflow https://stackoverflow.com/questions/18991388

  •  29-06-2022
  •  | 
  •  

문제

In a mac desktop app, I'm getting an "Unrecognized selector sent to instance" error.

Here's the line that causes the error:

    My_WebView *mvw = [mWebView LoadHtml];

And here's the relevant class definition:

My_WebView.h

    #import <WebKit/WebKit.h>  

    @interface My_WebView : WebView
    {
    }

    - (My_WebView *) LoadHtml; // initialize the WebView with a page

    @end

My_WebView.mm

    #import  "My_WebView.h"

    @implementation My_WebView

    - (My_WebView *) LoadHtml
    {
        NSLog(@"Loading HTML...");
        // do stuff...
        return self;
    }

    @end

When I run my code in the XCode debugger and break at the offending line, I'm told that mWebView is of the expected type (My_WebView). This rules out the problems identified in the many similar questions I've seen here.

Does anyone know what I'm doing wrong?

도움이 되었습니까?

해결책

I figured it out. I was confused. The instance wasn't the right type!

The XCode debugger said that mWebView was a pointer to a My_WebView.

    mWebView    My_WebView *    0x000000010e0884c0

This led me to think, wrongly, that My_WebView was also the runtime type of mWebView. It wasn't, as NSLog(@"mWebView: %@", mWebView); and po mWebView revealed.

     mWebView: <WebView: 0x1180ca4c0>

mWebView was actually just a plain old Cocoa WebView. That's why it wasn't responding to a LoadHTML message. Problem solved.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top