NemeSys
NemeSys

Reputation: 555

How to declare methods in objective c outside the interface?

When a method is declared in .h file is detected by intelisense and the warnings are not raised, when the method is used in .m file.

When a method is declared only in .m file, the intelisense doesn't detect it if is declared below the method where is being used.

To avoid the warnings there is a flag in xcode, but I prefer don't disable it.

There is any way to declare the methods in .m in order to be detected by intelisense and without the warning?

Thanks.

Upvotes: 1

Views: 1990

Answers (2)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

Two ways to fix it:

Either: Use a class extension to declare private methods at the top of the .m file:

@interface Foo ()

- (void)privateMethod;

@end

Or: Upgrade to Xcode 4.3.1, which contains a more recent version of clang. This newer version of the compiler does not need previously declared methods to call them in the same compilation unit.

Class extensions are still good for compatibility or to declare private properties, though.

Upvotes: 4

zneak
zneak

Reputation: 138081

You can use a category to declare additional methods on a class.

For instance, adding this at the top of your .m file:

@interface MyClass (PrivateCategory)

-(void)foo;
-(void)bar;

@end

will let Xcode know that MyClass additionally responds to foo and bar. The (PrivateCategory) tells the compiler that you're adding methods that should be "grouped" under the category PrivateCategory. You can pick whatever name you want, or even no name at all (although an "anonymous category" has slightly different semantics).

Upvotes: 1

Related Questions