5

I know that many methods need to call its super class method and some methods dont need,

I'm looking about sth about method swizzling.It's initialized in the load method,and in the tutorial there is no [super load].

i'm wondering if it's wrong or there is just no need to call [super load].

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        // When swizzling a class method, use the following:
        // Class class = object_getClass((id)self);

        SEL originalSelector = @selector(pushViewController:animated:);
        SEL swizzledSelector = @selector(flbs_pushViewController:animated:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

#pragma mark - Method Swizzling

- (void)flbs_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self flbs_pushViewController:viewController animated:animated];
    });
    NSLog(@"flbs_pushViewController");
}

By the way,this method is used to fix the navigation corruption.

I happened to reappear the problem sometimes,and i debuggered it,i think it's about the thread.So i make this swizzling to add sth in the system method.

If you can tell sth about the navigation corruption problem or this method swizzling,i also very appreciate that.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Henson Fang
  • 1,177
  • 7
  • 30
  • Best practice to call [super load] so all components get communication of event taking place. – nikhil84 Dec 31 '15 at 06:50
  • @nikhil84 Following any principle blindly is a great way to get yourself in a world of trouble. [Anyway,`+load` is a very special case.](https://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html) – dandan78 Sep 05 '18 at 13:36

1 Answers1

8

From the NSObject documentation (emphasis added):

A class’s +load method is called after all of its superclasses’ +load methods.

Which means that you don't have to call [super load] from your code. The load method from all superclasses has already been called by the Objective-C runtime before the method in your subclass is called.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382