OBJC运行时方法替换(Method swizzling)
- #import <objc/runtime.h>
- @implementation UIViewController (Tracking)
- + (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(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 {
- [self xxx_viewWillAppear:animated];
- NSLog(@"viewWillAppear: %@", self);
- }
- @end
- - (void)xxx_viewWillAppear:(BOOL)animated {
- [self xxx_viewWillAppear:animated];
- NSLog(@"viewWillAppear: %@", NSStringFromClass([self class]));
- }
OBJC运行时方法替换(Method swizzling)的更多相关文章
- Objective-C Runtime 运行时之四:Method Swizzling
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- Objective-C Runtime 运行时之四:Method Swizzling(转载)
理解Method Swizzling是学习runtime机制的一个很好的机会.在此不多做整理,仅翻译由Mattt Thompson发表于nshipster的Method Swizzling一文. Me ...
- iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字
方法一:设置BarButtonItem的文本样式为透明颜色,代码如下: [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegro ...
- 由objC运行时所想到的。。。
objC语言不仅仅有着面向对象的特点(封装,继承和多态),也拥有类似脚本语言的灵活(运行时),这让objC有着很多奇特的功能-可在运行时添加给类或对象添加方法,甚至可以添加类方法,甚至可以动态创建类. ...
- iOS动态运行时方法
在某些时候,程序可能需要根据获取的参数来决定调用的方法. 要实现这样的功能,就需要使用到动态运行时方法了. 首先需要定义好接口,以便调用. 然后就是动态调用定义好的方法. 这里有两种方法, 第一种: ...
- Android ART运行时无缝替换Dalvik虚拟机的过程分析
Android ART运行时无缝替换Dalvik虚拟机的过程分析 分类: Android2014-01-13 00:59 42722人阅读 评论(66) 收藏 举报 AndroidARTDalvikV ...
- (方法调配)Method Swizzling
一.概念 方法调配:因为Objective-C是运行时语言,也就是说究竟会调用何种方法要在运行期才能解析出来.那么我们其实也可以在运行时改变选择子名称.这样我们既不需要查看到源代码,又没有必要去重写子 ...
- ObjC运行时部分概念解析(二)
上篇文章简单的说明了两个关键字究竟是什么,这里主要讲讲ObjC中各种基本内存模型 Method typedef struct objc_method *Method; struct objc_meth ...
- ObjC运行时部分概念解析(一)
转型iOS已经许久了,Runtime(运行时)还没有好好了解过.之前没有阅读过源码,紧紧凭借自己的臆测.现在阅读下源码,做一些笔记.方便再次翻阅 SEL SEL是一个关键字,如果没有涉及runtime ...
随机推荐
- poj 3620 Avoid The Lakes【简单dfs】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6795 Accepted: 3622 D ...
- 基于ThinkPHP框架的简单的后台管理系统
版权声明:本文为博主原创文章,未经博主允许不得转载. 基于ThinkPHP框架的简单的后台管理系统 一个简单的后台管理系统,可能还不全面,可以自己改,有登录功能 实例如图:
- http://c7sky.com/works/css3slides/#1
http://c7sky.com/works/css3slides/#1 css3 学习
- UINavigationBar-使用总结
多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置. 1.获取导航条 UINavigationBar *navBar = self.navigationCo ...
- codechef Turbo Sort 题解
Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one ...
- PHP面向对象之旅:模板模式(转)
抽象类的应用就是典型的模版模式 抽象类的应用就是典型的模版模式,先声明一个不能被实例化的模版,在子类中去依照模版实现具体的应用. 我们写这样一个应用: 银行计算利息,都是利率乘以本金和存款时间,但各种 ...
- android 32 Gallery:横着滚动的列表
Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...
- windows使用python调用wget批处理下载数据
wget是linux/unix下通常使用的下载http/ftp的数据,使用非常方便,其实wget目前经过编译,也可在windows下使用.最近需要下载大量的遥感数据,使用了python写了批处理下载程 ...
- new Integer(1)和Integer.valueOf(1)的区别
java.lang包中的Integer类是我们比较常用的类,比如以下代码: Integer a=new Integer(1) Integer a=Integer.valueOf(1); 两个都是得到一 ...
- 使用EasyUI设计.net项目的菜单数实例
最近领导说我们之前的项目采用的菜单树模型过时了,现在采用EasyUI来设计了,于是学习了第三方资源库easyUI,发觉果然是好东西,这里给大家分享下. 首先到官网下载源文件,这个是开源的,都可以下再, ...