8

When and what methods should be declared in the @interface section of a class? As I understand, methods that describe what your class does should be declared in the @interface section, but other "helper" methods should not be declared. Is this a correct understanding from my side?

3 Answers 3

7

One way is to declare the instance methods in .h file. And, declare the private methods inside the .m, using a Category.

For example, in MyOwnClass.h file.

@interface MyOwnClass

- (void)aInstanceMethod;

@end

And, inside your MyOwnClass.m file, before the @implementation block,

@interface MyOwnClass (MyPrivateMethods)

- (void)aPrivateMethod;

@end
Sign up to request clarification or add additional context in comments.

2 Comments

NB The compiler won't check if the methods declared in a category are actually implemented. You can also use a class extension, which is like an anonymous category, except that the methods it declares must be implemented in the main @implementation block.
given that EmptyStack's idea is to create the category IN the implementation, as a convenient way of making methods private, then you should probably always use the extension as albertamg suggests: @interface MyOwnClass () ... there's no advantage in being able to optionally skip methods in the category since its only used in your class.
6

You usually should add your methods to the .h file when you want an external class to have access to it (public methods).

When they're private (only used internally by the class) just put them in your .m file.

Anyway, it's just a pattern. As Objective-C works with messages, even if you don't set a method in your .h file an external file can access it, but at least your auto-complete won't show it.

Comments

1

You should declare all your methods in your .h The tip from EmptyStack is nice but it's just a tip. If you don't intend to ship your binary as an SDK, you don't really need it.

Objective-C doesn't have (yet) private methods.

7 Comments

"If you don't intend to ship your binary as an SDK, you don't really need it". What?!
Well, from Wikipedia: "A software development kit (SDK or "devkit") is typically a set of development tools that allows for the creation of applications for a certain software package, software framework, hardware platform, computer system, ..., operating system, or similar platform." so I don't think it's the best term to use here. Apart from that, just because you know a method exists it isn't private. You don't need a compiled implementation to hide things. When we talk about private methods, encapsulation, ... , it's from the objects point of view, not necessarily from the programmer's.
I often use the tip that @EmptyStack provided.
if you want to release a piece of you application as a static library to enable others programmers to use your services (which seems to fit the SDK definition), you may want some methods not to be visible (from the programmer's point of view). As Objective-C doesn't offer private methods capability, there's no benefit to use “private categories”, from the objects' point of view (as explained by Raphael).
A SDK is way more than a library :-) but I see the point. I agree with you up to the last sentence. There are some benefits. For example, say you have multiple methods that performs similar tasks in your interface. Is theree any reason not to create a single method responsible for a common task in a private fashion an call it from the exposed methods' implementation?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.