I am trying to method swizzling for NSTimer in a category. But the swizzling method swizzling_invalidate
is never call when send message invalidate
to NSTimer object.
#import "NSTimer+Test.h"
#import <objc/runtime.h>
@implementation NSTimer (Test)
+ (void)load {
Method originalMethod, swizzlingMethod;
originalMethod = class_getInstanceMethod(self, @selector(invalidate));
swizzlingMethod = class_getInstanceMethod(self, @selector(swizzling_invalidate));
method_exchangeImplementations(originalMethod, swizzlingMethod);
}
- (void)swizzling_invalidate {
[self swizzling_invalidate];
}
@end
It is so strange that method swizzling for other NSObject is working. Like that:
#import "NSUserDefaults+Timing.h"
#import <objc/runtime.h>
@implementation NSUserDefaults (Timing)
+ (void)load {
Method original, swizzling;
original = class_getInstanceMethod([self class], @selector(synchronize));
swizzling = class_getInstanceMethod([self class], @selector(swizzling_synchronize));
method_exchangeImplementations(original, swizzling);
}
- (BOOL)swizzling_synchronize {
NSDate *start = [NSDate date];
BOOL returnValue = [self swizzling_synchronize];
NSLog(@"%f", [[NSDate date] timeIntervalSinceDate:start]);
return returnValue;
}
@end