Search code examples
iosobjective-cobjective-c-category

Objective C - Is it possible to call a category's method from another category of same type?


I would like to know if it is viable to actually call a category's method in another category of same type. I've tried doing so, but that does not seem to work. I want to know that is it a proper way or at least is it possible?

For ex:

CategoryA file

@implementation UIImage (UIImage+CategoryA)

-(void)doThis {
    NSLog(@"Something....");
}

CategoryB file

#import "UIImage+CategoryA.h"

@implementation UIImage (UIImage+CategoryB)

-(void)someMethod {
    [self doThis]; // Can I do this?
}


Any discussion/answers/insights are welcome.


Solution

  • A category adds methods to the original class. This is Objective-C so there's no concept of access control: once they exist, they are accessible to everybody. Therefore anybody with a pointer to an instance of the class and knowledge of the category can call them. That includes methods implemented in other categories of the same class.

    So, yes, it is possible.