I am learning method swizzling in Objective-C. Below is my code to swizzle
+(void)load{
NSLog(@"Load %@",[self class]);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(viewWillAppear:);
SEL swizzlingSelector = @selector(logging_viewWillAppear:);
Method origialMethod = class_getInstanceMethod(class, originalSelector);
Method swizzlingMethod = class_getInstanceMethod(class, swizzlingSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzlingSelector, method_getImplementation(origialMethod), method_getTypeEncoding(origialMethod));
}
else {
method_exchangeImplementations(origialMethod, swizzlingMethod);
}
});
}
-(void)logging_viewWillAppear:(BOOL)animated{
[self logging_viewWillAppear:animated];
NSLog(@"Logging viewWillAppear");
}
Everything is working fine. But the BOOL didAddMethod always returns NO. I would like to understand what is the scenario we will get didAddMethod = YES.