Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does method [load] need to call [super load]

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.

like image 495
Henson Fang Avatar asked Dec 31 '15 06:12

Henson Fang


1 Answers

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.

like image 118
Martin R Avatar answered Oct 10 '22 05:10

Martin R