What does the error “unrecognized selector sent to instance” mean in Xcode

后端 未结 10 618
谎友^
谎友^ 2021-01-11 09:28

What does it mean \"unrecognized selector sent to instance\" in Xcode?

10条回答
  •  攒了一身酷
    2021-01-11 09:42

    In my case it means I did not understand (for two days) a very simple requirement of the handler (selector, function): I had left off the ...:(NSNotification*)notification... in my selector (function).

    In the end it is just a self.stupidMistake (or programming tired while trying to understand a new thing in iOs/xCode). I read the docs at apple, I read many, many here at stackoverflow and read all kinds of other pages from the search results and just kept overlooking the fact that I had: in the viewDidLoad:

    [[NSNotificationCenter defaultCenter] addOberserver:self selector:@selector(myHandler:) name:@"UIApplicationWillResignActiveNotification" object:nil];
    

    in the .h (declaration) and .m (real code) I had invented:

    -(void)myHandler { ... }
    

    This generated the unrecognized selector sent to instance (crash and debug output) at runtime (no errors or warnings in xcode). Then I spent almost two whole days trying to figure out the error and the error was:

    -(void)myHandler:(NSNotification*)notification { ... }
    

    Hope it helps anyone else stuck - it is a syntax thing (your Selector or Handler or Function or whatever you want to call it) must take a (NSNotification*) 'object' as a parameter whether you use it or not; and xcode (4.2 w/iOs SDK 5.0) does not generate any errors or warnings about this 'mistake'.

提交回复
热议问题