0

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?

Community
  • 1
  • 1
dB'
  • 7,838
  • 15
  • 58
  • 101
  • How are you instantiating mWebView? Are you checking that mWebView is not nil? – neilco Sep 24 '13 at 20:30
  • 1
    Did you actually log the instance, the type you saw may be a lie... – Wain Sep 24 '13 at 20:32
  • I'll try that... how do I log the instance? (I'm a major Cocoa newbie.) – dB' Sep 24 '13 at 20:32
  • Re: logging, ok, got it: `NSLog(@"mWebView: %@", mWebView);` I'll try that and get back to you. – dB' Sep 24 '13 at 20:46
  • Or from the debugger, `po mWebView` (assuming that you're in a stack frame where `mWebView` is accessible). You can log the result of any expression using either `po` (for objects) or `p` (for non-objects). – Chris Devereux Sep 24 '13 at 20:47

1 Answers1

0

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.

dB'
  • 7,838
  • 15
  • 58
  • 101