I am currently creating my first iPhone app in Objective C and getting a lot of warnings because I've in many places taken advantage of the fact that Objective C is a dynamically typed language. I get a lot of 'class' may not respond to '-message:' warnings. Should I strive to remove all these warnings? If yes, then what's the benefit of having a dynamic language at your disposal?
The Objective-C compiler automatically inserts casts into the binary code where the arguments and return types are of different types (e.g. different size integers). If the compiler can't unambiguously figure out the signature of the method you're using at compile time, it's possible it can get this wrong leading to very weird bugs.
(This is why the compiler also has special warning messages for "no method found" and "multiple methods found", since in those cases it's even more likely to get it wrong)
Yes, get rid of the warnings. If you're sending a message to an object, then you must be pretty confident that the object does, in fact, have a method by that name. Why not let the compiler know that as well? Mostly, this just means using appropriate types for your object pointers.
Say you have a pointer of type
Another situation is where you have several objects of different types that all happen to implement methods with the same name. In that case, changing the pointer type doesn't seem such a great option because you don't know what type of object you might be dealing with; you only know that any object you deal with supports the message your sending. In that case, declare a protocol containing whatever method or methods you're sending, and declare the pointer as a pointer to an object implementing that protocol, like:
In both cases above, you don't lose any dynamism by fixing the warning. You're just sharing with the compiler your knowledge of the restrictions that already exist. You're also making your code more explicit, which makes it easier to maintain.
Most important, by eliminating these easy-to-fix warnings, you'll make it easier to spot other warnings that might alert you to a more significant problem.
'Dynamic' here really just means that the ability of an object to respond to a message isn't known until run time. If you were writing in, say, C++, you'd get an error instead of a warning during compilation because the compiler would know for certain that class X does not implement method Y. In Objective-C, you still have to make sure that objects can respond to the messages you send them, but the error you get if you fail in that regard doesn't come until you run the program.
I can't really answer the question about Objective-C being really dynamic or not. I think it stays in the middle of dynamic and static world.
However, keeping warnings is rarely a good practice, as real warnings you are interested in are lost in the middle of a lot of warnings you don't care about.
So, you should get rid of these warnings. There are several things you can do :
Disclaimer : I don't say all of these points are good practices, but they are technical ways of achieving the goal of getting rid of warnings