I understand that overriding a method by using a category is a discouraged practice. Nonetheless, I have to deal with some code that does this. When I ran the following code, I was initially surprised that my category method was called in both cases, though thinking about how the linker must have to make a decision at link time for any given symbol made me feel better about it.
My question: Assuming no other categories are in play other than the ones I create, am I guaranteed that the implementation in my category will always be the logic that is called as long as the header for it is imported from somewhere?
someObject.h
#import <Foundation/Foundation.h>
@interface SomeObject : NSObject
- (void)doSomething;
@end
someObject.m
#import "SomeObject.h"
@implementation SomeObject
- (void)doSomething
{
NSLog(@"Original");
}
@end
someObject+Cat.h
#import <Foundation/Foundation.h>
#import "SomeObject.h"
@interface SomeObject (SomeObject)
- (void)doSomething;
@end
someObject+Cat.m
#import "SomeObject+Cat.h"
@implementation SomeObject (SomeObject)
- (void)doSomething
{
NSLog(@"New!");
}
@end
someObjectUser.h
#import <Foundation/Foundation.h>
@interface SomeObjectUser : NSObject
- (void)useSomeObject;
@end
someObjectUser.m
#import "SomeObjectUser.h"
#import "SomeObject.h"
@implementation SomeObjectUser
- (void)useSomeObject
{
[[SomeObject new] doSomething];
}
@end
Test.m
- (void)testExample
{
[[SomeObject new] doSomething];
[[SomeObjectUser new] useSomeObject];
}
Result
2013-02-28 11:32:37.417 CategoryExample[933:907] New!
2013-02-28 11:32:37.419 CategoryExample[933:907] New!