objective-c warning ‘class’ may not respond to ‘method’

This is a very interesting error message, you may want to look at the order of the method you are calling, and the code that causes the warning message. 
-(void) aMainMethod{
 [self testMethod] //call the method, warning occurs here
 ....
}
//here's the method you are calling
-(void) testMethod{
  NSLog(@"test");


If  your method is at somewhere after the code you call it (if it’s like above), move it so that it’s before the code (like the code below), and the warning should go away. Even with the warning, you should still be able to compile and run without any problem. 

//here's the method you are calling
-(void) testMethod{
NSLog(@"test");
}
-(void) aMainMethod{
[self testMethod] //no warning
....
}

Here’s an article about this with more details.

Advertisements

24 thoughts on “objective-c warning ‘class’ may not respond to ‘method’

  1. Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language 😉
    See you!
    Your, Raiul Baztepo

  2. If you would have declared the function in the header of the file (the .h file) then it would not matter where the function body exists.

    Doing that will fix the errors as well. (Also the programming standard to declaring functions in C and Objective-C).

      • A better option would be to declare the functions in an “extension” (it is like a category that doesn’t have a name; i.e. you put nothing in the parentheses) at the top of the implementation file, not the header file. Functions declared in an extension can be implemented in the regular implementation section (unlike functions declared in a category that must be in a separate implementation section), so no change is needed to the implementation code. At the same time, you get the benefits of declaring it, and it is not in the header and thus does not clutter your interface.

  3. Thanks, this helped.

    FWIW, the problem appears to exist at the file level as well: class #0 in the file order calling a ‘-method’ from class #1 in the file order caused this problem for me. Reordering the files fixed it.

    • @peter, that was sarcasm, right?

      unfortunately, after 2 years working in a flex/AIR team I fear it wasn’t sarcasm but deadly serious, because I know other flex developers would share your view.

      imo actionscript isn’t bad, it just has one huuuuuuuge huge disadvantage you’ve overlooked: Flash!!!

      that’s why it’s not on the iphone and rightly so. how beautiful it is to be protected against one of the most bloated, outdated, slow and buggy technologies still hanging around. developer community did a good job to bury IE6, we should similarly be looking to bury Flash, not sing it’s praises just out of principle. This is not 1998 anymore but 2010 and Flash is waaaaaay past it’s sell by date!

      • You speak true my friend, I’m in the Flash business for almost a decade now (8 yrs) so, I’m no Apple fan boy (as some fellow developers would judge) but I agree, Flash it’s way past it’s sell date, we need to move on, explore new technologies. The truth is, right now the only thing that keeps Flash for being bury it’s that there is no other real alternative, unfortunately.

  4. Pingback: Stanford 02a « calu's iPhone ontwikkelingen

  5. Thank you Emily for pointing out the obvious that is not always so obvious to non C users like me.

    Once I placed the function ahead of the code that uses it, the warning went away.

    Makes perfect sense…the order does matter, even though the code will compile and run with the warning.

    But sometimes we forget 🙂

  6. Pingback: Objective C中的 may not response to … warring - Anistar RIA Blog

  7. Thanks a lot! I was going crazy about this warning.
    This should be obvious, except for people starting to learn Objective-C (like me). I used to program in C but I have been programming in Java only for the last 5 or 6 years, so the notion of order of declaration being important seems strange now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s