tags:

views:

29

answers:

4

Hi, top coders

when I compile my app project via xCode , I got too many warning, most say : 'myClass' may not respond to '+isAliveNetworkConnection:' , although the method 'isAliveNetworkConnection' was implemented in the .m file but it does not declare in .h file , I find it works so I do not like to rewrite in the head file again;

I think the Objective C is not convince to use than Java ... maybe I am too lazy ? :D

does anyone have the same thought ?

A: 

C and Java are different. You have to tell the compiler what methods your class implements in the interface, otherwise it has no way of knowing, which is why you get the warnings.

Jason Coco
Hi,thanks for your answered, I know that is different between C and Java syntax ... maybe my question is nonsense, I just want the develop tools could be more easy to use and can upgrade if possible .
Robin
+3  A: 

Objective-C is a dynamic language. It's perfectly fine(at compilation time) to send a message to a class that doesn't implement the appropriate method. You can also add methods to classes at runtime! Objective-C will let you get away with a lot of stuff. However, you generally should avoid such practices, unless you have a very good reason for doing so.

As you've found, you're "allowed" to skip declaring methods in your .h file, but it's not recommended. Why? First, if someone else is reading your code, it can be easier to get a quick list of all of the methods implemented. Second, if you're debugging your code, and getting a crash in, say objc_msgsend, where an object is being sent a message it won't respond to, you can easily track down the issue because it will occur as a warning in your code. If you always avoid naming functions in your .h file, you'll have hundreds(thousands?) of warnings, and it won't be easy to sift through them all.

In short, you can get away with a lot, but you should generally follow "good" coding practices.

Mike
really helpful... with the dynamic update the Class Behavior. great idea , like with this feature .thanks Mike
Robin
also thanks all coders, :-)
Robin
A: 

You need to declare all methods that you use in your header file if you want this warning to go away. The compiler doesn't know about the methods in your .m file unless you do this.

You can implement private methods that you don't want in the header by using a category, like so:

in YourClass.m:

@interface YourClass (Private)
- (void)yourPrivateMethod;
@end

@implementation YourClass

- (void)yourPrivateMethod
{
    //do stuff
}
@end
Rob Keniger
A: 

The header file is the official API for your class. If you do not specify something there, the compiler will assume that other code using that selector is using some internal functionality.

The reason it gives you a warning is that doing so by mistake risks adding unwanted side effects to your code should the one maintaining the class who's unofficial API you are using decide to change something without knowing that someone else is depending on the selector. (and while writing this, Mike gave a much more complete answer).

Take my advice, don't be lazy. Unless you are very experienced and know where it is ok to be lazy the risk is rather high it will come back and bite you later.

Fredrik