3

I started programming in C++. Because of this, I was taught early on that for each definition, there should be a declaration. With member functions, it is a requirement.

Now that I have started learning Objective-C, I was somewhat "shocked" to find out that you don't need to pair them up. Obviously, if you don't define a method, it will not be called. If you don't declare a method, but define it, it will be called.

What's the rationale for allowing this type of ad-hoc definitions? Since I'm biased by C++, I find it annoying to read other people's code, as many times, the methods aren't declared. It's hard to get an overview of the class's interface.

I found a couple of answers here: Objective c: method relation .h and .m. Unfortunately they're not very conclusive.

Community
  • 1
  • 1
Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51

1 Answers1

4

Objective-C and other languages include meta-programming tooling that allows you to define methods dynamically, so you could declare a specific method and have something else define it in runtime, you can see a classic example of this feature on this question about how to implement method_missing like functionality on Objective-C.

As for the other end, having methods implemented but not declared, there's no definite way to define private methods on Objective-C and from time to time you want to define methods that are known only to your own classes and not for everyone else. So, you don't declare the method in your .h file, but you implement it nonetheless at your .m file.

The compiler will usually complain that you are using an undefined method, but you can either ignore this or declare these private methods at your .m file as a special category, it would look like this:

@interface YourClass (PrivateMethods)
  - (void) somePrivateMethod;
@end


@implementation YourClass 
  - (void) somePrivateMethod { NSLog(@"Something!"); }
@end

Common patterns like Proxies (local or remote ones) usually rely on these meta-programming tooling to be available and they are quite common in languages like Java, Ruby, Python and Objective-C itself.

Community
  • 1
  • 1
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • I have this book that claims you can use `@private` to protect your methods... Is it just for show? – Jörgen Sigvardsson Aug 17 '11 at 20:28
  • 1
    @private is for variables, not methods. Objective-C, like it's indirect parent, Smalltalk, does not have the notion of private methods. – Maurício Linhares Aug 17 '11 at 20:33
  • In ObjC2, you should no longer put "PrivateMethods" in the parentheses of your private API category (now "extension"). This word is just a comment, but including it causes the compiler to not check that `somePrivateMethod` is implemented. If you just use `@interface YourClass ()`, then the compiler will force you to implement all of the methods in the `interface` block. This is a new feature in ObjC2, and a welcome addition. – Rob Napier Aug 17 '11 at 20:52
  • @Rob: Thanks! `@interface YourClass ()` is something I will definitely use to catch silly errors ASAP. – Jörgen Sigvardsson Aug 18 '11 at 06:40