桐庐城乡建设局网站,中国互联网站建设中心建站中心,视频号下载器手机版,网站开发和app的区别一般来说#xff0c;系统提供的方法已经足够开发了#xff0c;但是有的时候有些需求用普通方法不好做。 如#xff1a;在所有的viewcontroll 的viewwillappear#xff1a;方法之前打个log 你可能会这么做#xff1a; 1. 建一个uiviewcontroll 父类#xff0c;重写viewwil…一般来说系统提供的方法已经足够开发了但是有的时候有些需求用普通方法不好做。 如在所有的viewcontroll 的viewwillappear方法之前打个log 你可能会这么做 1. 建一个uiviewcontroll 父类重写viewwillappear方法调用super viewwillappear 方法之前加上log 2. 所有新建的uiviewcontroller 继承第一步生成的 确实你是完成这样的功能可是你做了那么多的修改基本每个uiviewcontroller都去修改了父类这种方法太过于笨重了 本文提供了简单地方法即可实现 我的理解中object-c 的类调用方法是根据三个元素来定义的。 1. 方法代表类定义中一个方法类型typedef struct objc_method *Method 2. SEL 选择器typedef struct objc_selector *SEL一个方法在运行时的名字常见的有 [self performSelector:selector(somemethod:) withObject:nil afterDelay:0.5]; selector(somemethod:)作为方法的入口 3. 方法的实现入口typedef id (*IMP)(id, SEL, …) 这三个元素确定了具体调用哪一个函数 直接看代码 [objc] view plaincopyprint? #import UIViewControllerTracking.h #import objc/runtime.h implementation UIViewController (Tracking) (void)load { NSString *className NSStringFromClass(self.class); NSLog(classname %, className); 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(xxx_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); } }); } 我们category重写了NSObject的 load 方法oc提供了objc/runtime.h类让我们获取这些东西同时还提供了对类方法操作的函数 我们想的是直接用一个方法替换掉系统的方法然后把一些自定义的动作加到方法中 我们只想运行一次就够了所以使用了 dispatch_once(onceToken, ^{ …… }接下来给类添加了新方法 把新方法和系统方法替换 [objc] view plaincopyprint? #pragma mark - Method Swizzling - (void)xxx_viewWillAppear:(BOOL)animated { NSLog(viewWillAppear: %, self); [self xxx_viewWillAppear:animated]; } 但是新方法实现的时候调用的是 [self xxx_viewwillAppear:animated]; 可能你会疑惑 这是因为我们在上面已经用xxx_viewwillAppear 和 viewwillAppear 互换了。所以实际上执行的是系统的viewwillAppear 这个时候可能你又有疑问了为什么实现是- (void)xxx_viewWillAppear:(BOOL)animated{} 这样的 这是因为 SEL swizzledSelector selector(xxx_viewWillAppear:); 拿的就是我们新写的方法。 可以结合这篇博客看配图很容易懂 http://blog.csdn.net/yiyaaixuexi/article/details/9374411 以及这篇对SEL讲的比较清楚 http://blog.csdn.net/fengsh998/article/details/8612969 代码下载地址 https://github.com/holysin/Method_swizzle 1 Method nameMethod class_getInstanceMethod(Class class,SEL name);//得到实例方法 参数:class类名 name 方法名 理解:通过类名和方法名来得到方法(c函数) 2 method_getImplementation(Method method) 参数:method 方法 理解:通过方法来得到它的实现 3 BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) 理解:将所给的name(方法名)的方法实现,被imp(方法的实现)代替 cls被添加方法的类 name可以理解为方法名这个貌似随便起名 imp实现这个方法的函数 types一个定义该函数返回值类型和参数类型的字符串这个具体会在后面讲 4 class_replaceMethod(Class class,SEL B, method_getImplementation(Method AMethod), method_getTypeEncoding(Method AMethod)); 参数:class :方法所属的类 B:将被替换其实现的方法名 AMethod:由A生成的方法(c函数) 理解:用A的方法实现来代替B的方法实现 5 method_exchangeImplementations(SEL A, SEL B); 理解:交换两个方法的实现 void swizzle_method(Class class,SEL originalSelector,SEL swizzledSelector) { Method originalMethod class_getInstanceMethod(class, originalSelector);//得到实例方法 Method swizzledMethod class_getInstanceMethod(class, swizzledSelector); BOOL didSwizzleMethod class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didSwizzleMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } 转载于:https://www.cnblogs.com/HughLiu/p/4648105.html