0

I'm creating a framework and I want to add private methods to a superclass inside a category so they are not exposed.

As an example let's say I have a public class defined as:

@interface Animal : NSObject

- (void)makeNoise;

@end

And a public subclass like:

@interface Dog : Animal

- (void)makeNoise;

@end

Now I want to add private methods related to all Animal objects so I create a category like:

@interface Animal(PrivateMethods) 

- (void)privateSing;

@end

And the problem comes here. Isn't it possible from the subclass to access the superclass PrivateMethods category?

If I run the following code, I get a unrecognized selector sent to instace error:

#import "Animal+PrivateMethods.h"
@implementation Dog

- (void)description {
    // unrecognized selector sent to instace
    [self privateSing];
}

@end
Esteban Bouza
  • 318
  • 1
  • 17
  • Is the category .m file included in your target? – jscs Oct 21 '12 at 17:50
  • Nevermind. Apparently this is only an issue when running the unit tests. When I import my framework in a separate project and run it, all the private methods are executed without any problem. – Esteban Bouza Oct 22 '12 at 08:10
  • 1
    Sounds like you may have essentially the same problem as http://stackoverflow.com/questions/11675256/ismemberofclass-returns-no-when-viewcontroller-is-instantiated-from-uistoryboard – jscs Oct 22 '12 at 18:17
  • @JoshCaswell Yes I was able to resolve it finally. I used this solution: http://stackoverflow.com/questions/5465076/unrecognized-selector-sentestcase-for-category which in the end is the same as you suggest. Apparently when you create a new category using XCode's 4.5.1 wizard, it's not added by default to the Tests compile sources phase. Thanks! – Esteban Bouza Oct 22 '12 at 20:20

0 Answers0