views:

92

answers:

5

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

+3  A: 

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.

Dave DeLong
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
A: 

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.

RyanWilcox
+1  A: 

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.

Darren
A: 

Usually, adding

@class myObject1

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

Elise van Looij
My, some people really don't like this answer. Any particular reason?
Elise van Looij
A: 

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.

Quinn Taylor
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
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