iOS开发那些事儿(四)the dark arts of the Objective-C runtime
一."Black Magic":Method Swizzling 利用 Runtime 特性把一个方法的实现与另一个方法的实现进行替换,也可以用runtime的四维理解——修改Dispatch Table让一个方法的IMP对应到我们指定的IMP上去
二.实例说明:比如我们想要在APP中记录每一个ViewController的出现次数
三.实例分析:
- 第一种思路就是在ViewController出现的一瞬间(viewDidAppear)我就用记录工具记录一条日志。
@implementation MyViewController () - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated]; // Custom code // Logging
[Logging logWithEventName:@“my view did appear”];
} - (void)myButtonClicked:(id)sender
{
// Custom code // Logging
[Logging logWithEventName:@“my button clicked”];
}暴力方法
*缺点就在于,我要记录所有的VC那么我就要在所有VC中去手写这些代码?那聪明的你一定会想到Catagory。但是仔细想一想你需要继承UIViewController、UITableViewController、UICollectionViewController所有这些 VC添加类别,而且违背了苹果设计Catagory的目的(为了想类添加一些代码而不是替换)
- 第二种思路就是我们今天的主角method swizzling
三.Demo:
#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);
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 {
//这里需要解释一下,因为我们在ViewwillAppear中需要去call父类的实现。这个这行代码调用的时候xxx_viewWillAppear与viewWillAppear方法已经替换。所以不是表面看上去的递归形式
[self xxx_viewWillAppear:animated];
[Logging logWithEventName:NSStringFromClass([self class])];
}
@end
为UIViewController添加分类
解释1.这里唯一可能需要解释的是 class_addMethod 。要先尝试添加原 selector 是为了做一层保护,因为如果这个类没有实现 originalSelector ,但其父类实现了,那 class_getInstanceMethod 会返回父类的方法。这样 method_exchangeImplementations 替换的是父类的那个方法,这当然不是你想要的。所以我们先尝试添加 orginalSelector ,如果已经存在,再用 method_exchangeImplementations 把原方法的实现跟新的方法实现给交换掉
解释2:Swizzling总是在+load中执行,Because method swizzling affects global state, it is important to minimize the possibility of race conditions. +load is guaranteed to be loaded during class initialization, which provides a modicum of consistency for changing system-wide behavior. By contrast, +initialize provides no such guarantee of when it will be executed—in fact, it may never be called, if that class is never messaged directly by the app.da大概意思是因为方+load是保证是在类初始化加载,从而改变系统行为提供了些许的一致性。相比之下,没有提供这样的保证+init(),如果APP没有直接调用,可能永远不会执行。
void (gOriginalViewDidAppear)(id, SEL, BOOL); void newViewDidAppear(UIViewController *self, SEL _cmd, BOOL animated)
{
// call original implementation
gOriginalViewDidAppear(self, _cmd, animated); // Logging
[Logging logWithEventName:NSStringFromClass([self class])];
} + (void)load
{
Method originalMethod = class_getInstanceMethod(self, @selector(viewDidAppear:));
gOriginalViewDidAppear = (void *)method_getImplementation(originalMethod); if(!class_addMethod(self, @selector(viewDidAppear:), (IMP) newViewDidAppear, method_getTypeEncoding(originalMethod))) {
method_setImplementation(originalMethod, (IMP) newViewDidAppear);
}
}
直接取代老方法而不是替换
四.总结:What are the Dangers of Method Swizzling in Objective C?
- Method swizzling is not atomic
- Changes behavior of un-owned code
- Possible naming conflicts
- Swizzling changes the method's arguments
- The order of swizzles matters
- Difficult to understand (looks recursive)
- Difficult to debug
iOS开发那些事儿(四)the dark arts of the Objective-C runtime的更多相关文章
- iOS开发RunLoop学习:四:RunLoop的应用和RunLoop的面试题
一:RunLoop的应用 #import "ViewController.h" @interface ViewController () /** 注释 */ @property ( ...
- iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...
- IOS开发之路四(UITabBarController)
前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者.今天看了一上午ios5基础教程这本书感觉有点头绪了....废话少说,讲一讲我上午做 ...
- iOS开发那些事儿(二)热补丁
一.热补丁作用:修复导致崩溃的错误.替换/增加方法.替换原来的界面等等 二.实现手段:JSPatch (使用Objective-C Objective-C和JavaScript jspatch桥.你可 ...
- iOS开发那些事儿(六)Git分之策略
git 分支策略 将要介绍的这个模型不会比任何一套流程内容多,每个团队成员都必须遵守,这样便于管理软件开发过程. 既分散又集中 我们使用的,且与这个分支模型配合的非常好的库,他有一个“真正”的中央仓库 ...
- iOS开发那些事儿(五)Objective-C浅拷贝与深拷贝
浅拷贝:copy操作出来的对象指针直接指向模板的地址.即两个对象公用一块内存地址 #import <Foundation/Foundation.h> int main(int argc, ...
- iOS开发-OC语言 (四)数组
知识点 1.NSArray 2.NSMutableArray 1.数组的基本用法: 2.数组的遍历 3.数组排序 =========== NSArray 不可变数组 ============= ...
- 【Swift 3.1】iOS开发笔记(四)
一.唱片旋转效果(360°无限顺时针旋转) func animationRotateCover() { coverImageView.layer.removeAllAnimations() let a ...
- 从零开始学ios开发(十四):Navigation Controllers and Table Views(上)
这一篇我们将学习一个新的控件Navigation Controller,很多时候Navigation Controller是和Table View紧密结合在一起的,因此在学习Navigation Co ...
随机推荐
- 关于JavaScript 原型的理解
原型的含义是指:如果构造器有个原型对象A,则由该构造器创建的实例(Object Instance)都必然复制于A.““在JavaScript中,对象实例(Object Instance)并没有原型,而 ...
- 深入浅出SlidingMenu
如果想直接查看源码的话可以从我的Github上下载查看:https://github.com/zhanghuijun0/demo-for-android/tree/master/SlidingMenu ...
- URI--http://zh.wikipedia.org/wiki/%E7%BB%9F%E4%B8%80%E8%B5%84%E6%BA%90%E6%A0%87%E5%BF%97%E7%AC%A6
维基百科,自由的百科全书 在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串. 该种标识允许用户对网络中( ...
- 二维数组在text,image的应用
NSArray *imageArr = @[@[@"查看地图",@"map_hy.png"], @[@"联系号码",@"phone ...
- JSON 日期格式带 T 问题
var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd"; object obj = new ...
- IT技术论坛
http://www.v2ex.com/ http://www.dewen.org/ http://stackoverflow.com/ http://segmentfault.com/ 博客园
- Spring容器关于继承的应用案例
一:案例演示 1.1.spring的配置文件applicationContext-extends.xml 1.2.业务类:CommonDao.java,PersonDao.java --------- ...
- 正确合理的建立MYSQL数据库索引
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整 ...
- Intent七大属性之总结
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
- 研究 Javascript的&&和||的另类用法
这篇文章主要介绍了Javascript的&&和||的另类用法,需要的朋友可以参考下 最近也没什么心思写文章了,感觉总有忙不完的事情,呵. 不过这些天又开始研究起 Titanium 来, ...