Take the 2-minute tour ×

I have found that very often, when calling myMethod asoociated with myObject1 from myObject2, I get the warning that "myObject1 may not respond to -myMethod", but then the program runs just fine anyway. Why doesn't the compiler recognize declared methods at compile time?

John Doner

share|improve this question
Which language? –  David Brown Dec 12 '09 at 4:08
Objective-C is my guess –  Artelius Dec 12 '09 at 4:11
Sorry, I'm using the IPhone SDK version of Objective-C. –  John R Doner Dec 12 '09 at 4:35
Dave's answer is likely correct, but vague. Post the code & error message! –  bbum Dec 12 '09 at 19:10

5 Answers 5

up vote 0 down vote accepted

One case where this happens frequently is if the type of the variable that contains the object is that of a superclass, and the method is only defined for the subclass. You can avoid this by either typing it as id or making the static typing more specific. If the variable type is that of the class itself, it's likely that the method isn't visible to the compiler in the scope where you're trying to invoke it — the other answers deal with this situation.

share|improve this answer
This comes close to describing my situation. I am calling view methods from the view controller in the syntax [self.view myMethod]; The compiler doesn't like it, but the program runs just fine. John Doner –  John R Doner Dec 13 '09 at 21:10
In that case you could probably call [(id)self.view myMethod], or just [(id)view myMethod] instead, unless your -view property doesn't just return your ivar. –  Quinn Taylor Dec 15 '09 at 4:48

This shows up as a warning because Objective-C is a very dynamic language. At compile time, the compiler hasn't found the declaration for "myMethod" (perhaps you're missing a header file, or forgot to include it in the header?) when it was attempting to compile the file. However, it only generated a warning because Objective-C has the ability to create and load extra methods at runtime so that by the time that code executes, that method will exist. Hence, it is only a warning.

Most likely you just haven't declare the method in the appropriate header file.

share|improve this answer
Well, I have done all necessary imports and declarations. But now that I think of it, the compiler is a one-pass compiler, so perhaps it has compiled the method-call statement before it reaches the method definitions in the associated class file. But it does seem like an import of an .h file at the top of a class would assure that the methods of that class would be recognised further down, where they're being called. –  John R Doner Dec 12 '09 at 5:06
"However, it only generated a warning because Objective-C has the ability to create and load extra methods at runtime" what? no. It runs only because the object actually supports that method (it exists). But you get the warning because the object's static type at compile time doesn't prove that. –  user102008 Jan 24 '12 at 3:09

Or sometimes, if you're using a delegate class, you need to define a category with those delegate methods in order for the compiler to find them.

share|improve this answer

The warning means you're calling a method for which the compiler has not yet seen a method declaration. It is an error in most other languages, and it is certainly a warning you cannot ignore.

If you haven't declared the method, do so in an @interface block at the top of your source file (if it's a private method) or in your class's header file (if it's a public method).

If you have declared the method in a header file, be sure to import the header file.

If you have declared the method and you are importing the correct header file, you have a typo somewhere.

share|improve this answer

Usually, adding

@class myObject1

will solve the problem. Check out Ben Gottlieb's answer to Objective-C @class vs. #import here on Stack Overflow.

share|improve this answer
My, some people really don't like this answer. Any particular reason? –  Elise van Looij Dec 18 '09 at 15:52

Your Answer


