RunTime--Method Swizzling

method swizzling 修改方法函数的调用

项目中用到的场景:打开页面显示的次数;

用到的函数:

@selector(方法名)
class_getInstanceMethod(<#类名#>, <#方法名(sel)#>)//返回类名下sel方法
class_addMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>, <#IMP imp#>, <#const char *types#>)//给cls添加一个新的方法,若干cls存在这个方法则返回失败
IMP class_getMethodImplementation(Class cls, SEL name)//返回cls的name方法的调用地址
IMP class_replaceMethod(Class cls, SEL name, IMP imp, 

                                    const char *types)//替换cls的name方法的指针
void method_exchangeImplementations(Method m1, Method m2)//交换两个方法的实现指针

 

代码:

+ (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(viewWillAppear:);

        SEL swizzledSelector = @selector(avc_viewWillAppear:);

        

        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);

        }

    });

}

- (void)avc_viewWillAppear:(BOOL)anmimation{

    [self avc_viewWillAppear:anmimation];

    NSLog(@"%@",self);

}

函数写在load方法里,load方法会在类初始调用,由于method swizzling会影响到类的全局状态,因此要尽量避免在并发处理中出现竞争的情况。+load能保证在类的初始化过程中被加载,并保证这种改变应用级别的行为的一致性。相比之下,+initialize在其执行时不提供这种保证—事实上,如果在应用中没为给这个类发送消息,则它可能永远不会被调用。

你可能感兴趣的:(Runtime)