jrswizzle icon indicating copy to clipboard operation
jrswizzle copied to clipboard

In origin method call super, the swizzling method will be called twice.

Open ghost opened this issue 8 years ago • 1 comments

@implementation UIViewController (Swizzling)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originSelector = @selector(viewWillAppear:);
        SEL swizzleSelector = @selector(sw_viewWillAppear:);
        [self jr_swizzleMethod:originSelector withMethod:swizzleSelector error:nil];
    });
}

- (void)sw_viewWillAppear:(BOOL)animated {
    [self sw_viewWillAppear:animated];
    NSLog(@"I'm swizzling method");
}
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"I'm origin method");
}
@end

When calling [super viewWillAppear:animated], the log--I'm swizzling method print twice. if not, print once. So, whether or not to need call super?

ghost avatar Dec 10 '16 04:12 ghost

How to fix this bug ?

CrazyRedcrownedcrane avatar Jun 18 '19 01:06 CrazyRedcrownedcrane