As @vikingosegundo suggested, you could use method swizzling:
#import "ClassA+Test.h"
#import <objc/runtime.h> // Needed for method swizzling
@implementation ClassA(Test)
-(void) swizzled_setImportantProperty:(id) newValue {
[self swizzled_setImportantProperty: newValue]; //it is NOT an endless recursion.
NSLog(@"%@, %@", newValue, [NSThread callStackSymbols]);
}
+(void)load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(setImportantProperty:));
swizzled = class_getInstanceMethod(self, @selector(swizzled_setImportantProperty:)); //UPDATE: missed a column here, sorry!
method_exchangeImplementations(original, swizzled);
}
@end
Here we declare a new method swizzled_setImportantProperty:
, and at runtime we swap it's implementation with setImportantProperty:
implementation. So that when we call setImportantProperty:
in code, the implementation for swizzled_setImportantProperty
will be called and vice versa.
That's why when we call swizzled_setImportantProperty:
inside the swizzled_setImportantProperty:
implementation, it won't call an endless recursion, because setImportantProperty:
implementation will be called. Just what we need.
UPDATE: Since overriding a +load
method might cause problems if it have already been implemented (or if might be implemented in the future by the library creators), there is a better option suggested by @JoshCaswell :
#import "ClassA+Test.h"
#import <objc/runtime.h> // Needed for method swizzling
@implementation ClassA(Test)
-(void) swizzled_setImportantProperty:(id) newValue {
[self swizzled_setImportantProperty: newValue]; //it is NOT an endless recursion.
NSLog(@"%@, %@", newValue, [NSThread callStackSymbols]);
}
@end
void swizzleSetImportantProperty(void) __attribute__((constructor))
{
Method original, swizzled;
original = class_getInstanceMethod([ClassA class], @selector(setImportantProperty:));
swizzled = class_getInstanceMethod([ClassA class], @selector(swizzled_setImportantProperty:)); //UPDATE: missed a column here, sorry!
method_exchangeImplementations(original, swizzled);
}