runtime系列(二) method swizzling 与AOP编程
来源:互联网 发布:jq删除数组中指定元素 编辑:程序博客网 时间:2024/05/29 18:31
什么是 AOP
: (site: baike.baidu.com),引用百度百科中的解释就是:
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要功能:
日志记录,性能统计,安全控制,事务处理,异常处理等等
主要意图:
将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。
iOS 开发中的 AOP
在 Objective-C 中,类的方法列表会把选择器的名称映射到方法的实现上,这样 动态消息转发系统 就可以以此找到需要调用的方法。这些方法是以函数指针的形式来表示,这种指针叫做 IMP 。
如下:
id (*IMP) (id, SEL, ...)
Objective-C
的 runtime
机制以此提供了获取和交换映射 IMP
的的接口:
// 获取方法Method class_getInstanceMethod(Class cls, SEL name);// 交换两个方法void method_exchangeImplementations(Method m1, Method m2)
我们可以通过上面两个方法来进行选择器和所映射的 IMP
进行交换:
来,直接上代码示例,比如我们的要实现功能是在每个控制器的 - viewDidLoad
方法里面log一下,一般有三种实现方式:
- 直接修改每个页面的 view controller 代码,简单粗暴;
- 子类化 view controller ,并让我们的 view controller 都继承这些子类;
- 使用
Method Swizzling
进行 hook,以达到AOP
编程的思想
第一种实现的代码是在每个类的里面都这么写:
- (void)viewDidLoad { [super viewDidLoad]; DDLog();}
第二种是只在基类里面写。然后所有的控制器都继承这个基类。
最后一种是最佳的解决方案:
@implementationUIViewController(Log)+ (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; SEL originalSelector = @selector(viewDidLoad); SEL swizzledSelector = @selector(log_viewDidLoad); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (success) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } });}
注意:
- 为什么使用
+ (void)load
?因为父类、子类和分类的该方法是分别调用,互不影响,而且是在类被加载的时候必定会调用的方法。
</div>
- runtime系列(二) method swizzling 与AOP编程
- Runtime系列(Method Swizzling)
- runTime(二)应用 Method Swizzling
- Method Swizzling、AOP 面向切片编程
- iOS开发之深入了解Objective-C Runtime、Method Swizzling、Method Swizzling 和 AOP 实践
- Runtime之Method Swizzling
- Runtime Method Swizzling
- Runtime Method Swizzling
- Runtime Method Swizzling
- Runtime(四)Method Swizzling
- Runtime之Method Swizzling
- RunTime之Method Swizzling
- ios runtime Method Swizzling
- iOS Runtime应用实例(二)method swizzling(方法交叉)
- (二)Method Swizzling 和 AOP 实践,(方法交换)
- Runtime 运行时:Method Swizzling
- Method Swizzling 和 AOP 实践
- Method Swizzling 和 AOP 实践
- struct和union在内存中占用空间大小的计算
- RPC实践(二)JsonRPC实践
- 密码强度判断
- Debian(Ubuntu) 中apt-get使用
- JQuery实现全选、全不选和反选功能
- runtime系列(二) method swizzling 与AOP编程
- Java关键字之synchronized
- wordpress笔记
- Oracle表空间操作/用户权限/导入导出
- Failed to apply plugin [id 'com.android.application']
- java compiler level does not match the version of the installed Java project facet 解决方法
- netstat 统计网络连接数
- JavaScript parseInt() &parseFloat & toFixed函数
- Hadoop Snappy安装终极教程