The following is working in objective c:
// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject
- (NSString *) myMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
@end
//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB)
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end
The question is, if I am just importing ClassA.h and send the message
[myClassA myMethod]; //returns B
why is this returning B
? I am not importing ClassA+CategoryB
Even futhrer, if I did the following:
// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject
- (NSString *) myMethod;
- (NSString *) mySecondMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
- (NSString *) mySecondMethod { return [self myMethod]; }
@end
//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB)
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end
and call mySecondMethod:
ClassA *a = [[ClassA alloc] init];
NSLog(@"%@",[a myMethod]);
the result will still be B
although nobody knows (due to no import) of the category implementation?!
I'd excepted, only to return B
if I was importing the category...
So any hints appreciated.
Objective-C messaging is dynamic, this means that it doesn't matter if you import or not the category. The object will receive the message and execute that method.
The category is overriding your method. This means that when the runtime sends a message to that object, will always find the overridden method, no matter what you import.
If you want to ignore a category you shouldn't compile it, so you could remove the category from the compiler sources.
An alternative is subclassing.
Also read this:
So in your case you haven't got any problem because as stated, this is less likely to happen with user defined classes. But you should definitely use subclassing instead of writing a category.
Obj-C allows you to add methods to an existing class using Categories. So if you add a method to NSString, the categoriesed method is available to NSMutableString and all classes which inherits NSString or any subclasses of NSString.
But you should avoid to Override the category.
You will not be 100% sure which method will be called. It depends on compiler.
From Apple Documentation.
You implicitly included the code defined in the category by compiling it.
If you want to avoid the category code to be executed you should remove it from your target, by removing the category implementation file. You can do that from
Target->Build Phase->Compile Sources
That said, you should never use a category to override a method. That's a very bad practice and it's not what categories are for.