Is it possible to declare a second @interface for a category?

I am trying to declare a private @interface for a category in a .m file.

For a normal class, I would do:

 @interface ClassA () @end @implementation ClassA @end 

and it will work smoothly.

For a class with categories I tried:

 @interface ClassA (CategoryA) () @end @implementation ClassA (CategoryA) @end 

but he gives all kinds of mistakes. I am trying to "expand" a category, a way to extend a class through this @interface ClassA () syntax.

I want to have private methods for this category, and I would like to know if in INDITION to the open interface I can put the second @interface category in a .m file that does not expose instance variables and methods outside the class itself.

Something like that:

ClassA + categoryA.h

 @interface ClassA (CategoryA) <some public methods> @end 

ClassA + categoryA.m File

 @interface ClassA (CategoryA) <some private methods> @end @implementation ClassA (CategoryA) <here I want to be able to call the private methods above> @end 

This now gives me a warning in Xcode:

Duplicate definition of category 'CategoryA' on interface 'ClassA'

Is there any way to get this behavior?

+7
source share
3 answers

No, you cannot declare two interfaces for one category. You can do one of two things:

Englebert + Humperdinck.h

 #import "Englebert.h" @interface Englebert (Humperdinck) - (void) croon; @end 

You can declare another category with a different name to contain private methods. Then they can be used in the same file where the private category interface is declared:

Englebert + Humperdinck.m

 #import "Englebert+Humperdinck.h" @interface Englebert (HumperdinckPrivate) - (void) warmUp; @end @implementation Englebert (HumperdinckPrivate) - (void)warmUp { NSLog(@"Warm up"); } @end @implementation Englebert (Humperdinck) - (void)croon { [self warmUp]; NSLog(@"Croon"); // etc. } @end 

Another option is to simply not declare private methods. If you simply define them in the implementation block, you can use them at any point in this file after they are defined (and for the latest version of Xcode / LLVM, the order is actually not important) - undeclared methods can be used anywhere in the file in which they are defined ) No other files will be able to see these methods.

Englebert + Humperdinck.m

 #import "Englebert+Humperdinck.h" @implementation Englebert (Humperdinck) /* Undeclared private method */ - (void)warmUp { NSLog(@"Warm up"); } - (void)croon { [self warmUp]; NSLog(@"Croon"); // etc. } @end 
+12
source

Do

 @interface ClassA (CategoryA) @end @implementation ClassA (CategoryA) @end 

Instead. There can be no instance of varibles in categories. And what mistakes are you talking about?

+1
source

@interface ClassA () is an anonymous category , and you can use them as interfaces and define an implementation in an implementation of ClassA . @interface ClassA (CategoryA) () is a syntax error and should read @interface ClassA (CategoryA)

EDIT:

To create private methods for a class, in the .m file of this class you would:

 @interface ClassA () // Private functions declared @end @implementation ClassA // Private functions defined // Other functions defined @end 

The same can be done for the named categories, however you will need to define the implementation separately to avoid warnings. Again, in the .m file:

 @interface ClassA (hidden) // Private functions declared @end @implementation ClassA (hidden) // Private functions declared @end @implementation ClassA // Other functions defined @end 
0
source

All Articles