App Crashes: unrecognized selector sent to instance?!

Discussion in 'iOS Programming' started by MacBook MH, Nov 18, 2010.

  1. MacBook MH macrumors member

    Mar 7, 2009
    In my navigation based app:
    Conslole message:
    MyApp[8312:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController secondView]: unrecognized selector sent to instance 0x5f49c20'

    After compiling my app with NO errors, I launched it, but when I clicked an item in the Table View the app in the simulator suddenly crashes and I get the error message into the console (above).
    I'm totally lost and I don't know my way around it...
    Any suggestions?
  2. cnstoll, Nov 18, 2010
    Last edited: Nov 18, 2010

    cnstoll macrumors 6502

    Aug 29, 2010
    Unrecognized Selector means you're trying to send a message to something that doesn't know what to do with it. There's a few things that can cause this.

    1) The object you're sending it to doesn't implement that method, or more likely, it's not the type of object that you think it is, or it's not actually an object.

    2) The object you're sending a message TO (in this case RootViewController) does not exist, or is nil.

    It depends on what RootViewController is. Can you show us how that is defined?

    To me it looks like you're trying to send a message to a CLASS, and not to an OBJECT, because judging by that segment that's what RootViewController looks like it is. If that's the case, and you have already declared an instance of RootViewController, then try just sending that message to the instance of RootViewController rather than to RootViewController itself.

    Generally that would look something like this:

    // Create an instance of class RootViewController
    RootViewController *rootViewController = [[RootViewController alloc] initWithNib:<NibName>];
    // Message
    [rootViewController secondView];
    But yeah, if that doesn't make sense, just show us some of the code.

    Also, a great way to find these problems is the Debug tool within Xcode. Figure out the section of code where you think the problem could be, and place a breakpoint (double click in the left hand side of the frame next to the line where that code is, like the line at the top of the method where that is being called). Then when you "Build and Debug", run through your app like you would to reproduce this error, and the program will stop when it hits that line. Then it will enter debug mode, allowing you to step through line by line and see the state of your local and instance variables. This is a great tool and makes it really easy to find problems like this.

    You can also try the "Build and Analyze" option in Xcode from the build menu. I wouldn't be surprised if that spotted the issue for you.
  3. seepel macrumors 6502


    Dec 22, 2009
    I'm going to nit pick here so people don't get the wrong idea. It is A-OK to send messages to a nil pointer. The problem comes when you have an invalid pointer to an object, then you end up sending messages to random memory. Sometimes you'll hit an object and get a similar error to the one in this thread and sometimes you'll get a segfault, and worse sometimes you might get your expected behavior!

Share This Page