Calling an inherited method causes warning without cast to superclass
I have the following inheritance tree:
NSObject <- GameObject <- RenderableObject <- SimpleBullet
GameObject
has a method called bounds
that returns the object's bounds. In my code开发者_Python百科 I have a SimpleBullet
that calls bounds
to get its bounds, and I receive a warning saying bounds
is defined in several places; odd. If I cast the SimpleBullet
to a GameObject
and call the bounds
method, everything works as expected. What's happening? I can't figure out this behaviour. Example:
SimpleBullet* bullet = bulletInstance;
[bullet bounds]; // we get the warning.
[(GameObject*)bullet bounds]; // works as expected.
As I said, the bounds
method is defined in GameObject
, but why is Obj-C not aware that SimpleBullet
is a GameObject
and not allowing me to call its method without the warning?
After some investigation I managed to know what was happening. The compiler was playing tricks on me because did not report a meanfull error. The problem was that I wasn't importing/including SimpleBullet.h. A forward declaration was declared but not the real implementation.
This, leads me to think that by default compiler treats it as an id object, and tries to find a method among all registered classes that suits the call signature. Just a guess, not sure about this :).
Thanks for all the help.
UIView
also has a -bounds
method. Make sure your method has the same return type.
精彩评论