0

Possible Duplicate:
Private Method Declaration Objective-C

I assumed in objective-c class methods need to be declared either in the .h file which makes them publicly visible or in the .m file using class extension to make it more private.

I thought without first declaring the method, xcode would complain, however I add a method to my main AppDelegate class without declaring it, and everything works fine.

What part have I confused, should I be declaring all methods of the class or is it okay not to if the method will only be used by that class and no where else??

Community
  • 1
  • 1
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59

3 Answers3

1

You declare methods anyway. Either in .h file

@interface ViewController : UIViewController
-(void)myMethod;
@end

or in private interface in .m

#import "ViewController.h"

@interface ViewController ()
-(void)myMethod;
@end
Stas
  • 9,925
  • 9
  • 42
  • 77
  • 1
    With current Xcode versions, you don't have to "forward declare" methods anymore, if they are defined in the same compilation unit. – Martin R Oct 25 '12 at 09:46
  • hmm..don't think that's a good practice.. – Stas Oct 25 '12 at 10:03
  • I don't see a problem. The compiler is just smarter and checks the remaining implementation block to find a matching prototype. You will still get warnings or errors if the call does not match the prototype. – Martin R Oct 25 '12 at 10:06
0

Declaration of all class methods is not necessary in .h file. Declare only those methods in .h file which you want to make publicly accessible to others.

kidsid49
  • 1,358
  • 3
  • 18
  • 37
0

Objective C is very dynamic language, and it resolves methods at runtime. That's why we're sending messages to objects and not invoking methods (like for example in C++). So if compiler doesn't see method declaration it doesn't mean that object can't find it at runtime.

So you can actually define ObjC methods everywhere you want (in any file or even in different libraries). Ones the program is compiled and linked ObjC runtime can find all of them.

Max
  • 16,679
  • 4
  • 44
  • 57
  • That is only partially correct, at least if you compile with ARC. To compile `[myInstance someMethod]` the ARC compiler needs the signature of `someMethod`, so you have to declare the prototype somewhere. – Martin R Oct 25 '12 at 09:45
  • Does that mean, I dont have to define the method. When should I define a method like the documentation shows, and when can I get away with not defining it at all – Ben_hawk Oct 25 '12 at 09:45
  • @Martin R that's why I don't like ARC :) – Max Oct 25 '12 at 09:51
  • You have to define the method always. You may not declare it's prototype. As for me I always declare public methods in .h file and private ones in .m Then I define them in a single @implementation block – Max Oct 25 '12 at 09:53
  • 1
    @Ben_hawk: Have a look at the accepted answer of the "possible duplicate" to your question. – Martin R Oct 25 '12 at 09:55
  • @Max: Event without ARC you get a compiler warning if you compile a method call with unknown selector. But we don't want to start a new "ARC or not ARC" discussion :-) – Martin R Oct 25 '12 at 10:00