zoukankan      html  css  js  c++  java
  • method swizzling

    
    

    #import
    <objc/runtime.h> @implementation UIViewController (Tracking) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; SEL originalSelector = @selector(viewWillAppear:); SEL swizzledSelector = @selector(xxx_viewWillAppear:); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); // When swizzling a class method, use the following: // Class class = object_getClass((id)self); // ... // Method originalMethod = class_getClassMethod(class, originalSelector); // Method swizzledMethod = class_getClassMethod(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)xxx_viewWillAppear:(BOOL)animated { [self xxx_viewWillAppear:animated]; NSLog(@"viewWillAppear: %@", self); } @end

    matt

  • 相关阅读:
    常用的设计模式汇总
    设计模式总结
    C# WinForm文章收集
    SQL Server 2012/2016/2017 新增函数
    SQL Server 日期函数大全
    【BZOJ3622】已经没有什么好害怕的了
    【SDOI2009】Bill的挑战
    【HDU4507】恨7不成妻
    BSOJ 2423 -- 【PA2014】Final Zarowki
    BSOJ 4591 -- 【JLOI2015】城池攻占
  • 原文地址:https://www.cnblogs.com/pencilCool/p/4675232.html
Copyright © 2011-2022 走看看