runtime这个东西,项目是很少用到的,但面试又避不可少,了解其内部的机制对底层的理解还是很有必要的。

1.动态添加属性

拓展类别属性的简单实现

a.定义字面量指针  static char dynamicAttributes;

b.设置属性  objc_setAssociatedObject(self,&dynamicAttributes,(id)value,OBJC_ASSOCIATION_COPY_NONATOMIC|OBJC_ASSCOCIATION_RETAIN)

c.获取属性值  objc_getAsscociatedObject(self,&dynamicAttributes)

2.model转字典

每个类中都有一个属性列表,我们要做的就是遍历该列表,利用KVC取值,装入字典

unsigned int count = 0;
objc_property_t *ptyList = class_copyPropertyList(self.class,&count);
NSMutableDictonary *dic = [NSMutableDictionary dictionary];
for (int i=0;i<count;i++) {
objc_property pty = ptyList[i];
const char *cname = property_getName(pty);
NSString *name = [NSString stringWithUTF8String:cname];
if (name.length > 0) {
id value = [self valueForKey:name];
[dic setObject:value forKey:name];
}
}

3.方法交换

C中方法是编译时就得实现。OC中方法是编译时无需实现,可在运行是动态插入方法及实现。利用这一点,可以轻松实现运行时方法的动态交换。

因为load方法早于main方法,并且不会覆盖父类实现,为了提高代码的可读性,一半是在分类中实现相关方法的交换。

+ load {
SEL original = @selector(willMoveToSuperView:);
SEL exchange = @selector(gl_willMoveToSuperView:);
Method originalMethod = class_getInstanceMethod(self.class,original);
Method exchangeMethod = class_getInstanceMethod(self.class,exchange);
// 动态添加方法,若选择器存在方法实现,会失败
BOOL isAdd = class_addMethod(self,original,method_getImplementation(exchangeMethod),method_getTypeEncoding(exchangeMethod));
if (isAdd) {
// 添加成功,将新的选择器替换为旧实现
class_replaceMethod(self,exchange,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
}else {
// 直接交换两方法实现
method_changeImplementations(originalMethod,exchangeMethod);
}
} - (void)gl_villMoveToSuperView:(UIView *)superView {
// do something ...
}

isa指针

  是一个指向所属类的指针。OC中消息机制是依靠objc_msgSend(receiver,selector)这个函数发送消息的。

  objc_msgSend会根据实例对象的isa指针查找对象的类,然后查找该类的dispatch_table中的selector,找不到就依次查找父类,直到NSObject类。实在找不到方法就抛出异常。找到selector后,会根据dispatch_table中的内存地址该selector。为了提高转发效率,系统会将所有的selector地址和已使用的selector地址缓存起来,通过类的形式划分不同的缓冲区域。obj_msgSend去查找dispatch_table前,会先检查该类的缓存,如果缓存命中,就直接调用selector。

消息转发机制(前提是dispatch_table找不到对应的selector)

  1.对象收到无法解读的消息后,首先会调用resolveInstanceMethod:询问是否有动态添加方法来处理

void speak(id self, SEL _cmd){
NSLog(@"Now I can speak.");
}
+ (BOOL)resolveInstanceMethod:(SEL)sel { NSLog(@"resolveInstanceMethod: %@", NSStringFromSelector(sel));
if (sel == @selector(speak)) {
class_addMethod([self class], sel, (IMP)speak, "V@:");
return YES;
}
return [super resolveInstanceMethod:sel];
}

  2.第一步若没有添加新方法,那就询问有没别人帮忙处理,调用的是forwardingTargetForSelector:

- (id)forwardingTargetForSelector:(SEL)aSelector {
NSLog(@"forwardingTargetForSelector: %@", NSStringFromSelector(aSelector));
Bird *bird = [[Bird alloc] init];
if ([bird respondsToSelector: aSelector]) {
return bird;
}
return [super forwardingTargetForSelector: aSelector];
}
// Bird.m
- (void)fly {
NSLog(@"I am a bird, I can fly.");
}

  3.当前两步均没有响应时,走到第三部,调用forwardInvocation:,该方法会首先调用methodSignatureForSelector:方法获取选择子的方法签名

- (void)forwardInvocation:(NSInvocation *)anInvocation {
NSLog(@"forwardInvocation: %@", NSStringFromSelector([anInvocation selector]));
if ([anInvocation selector] == @selector(speak)) {
Monkey *monkey = [[Monkey alloc] init];
[anInvocation invokeWithTarget:monkey];
}
} - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSLog(@"method signature for selector: %@", NSStringFromSelector(aSelector));
if (aSelector == @selector(code)) {
return [NSMethodSignature signatureWithObjCTypes:"V@:@"];
}
return [super methodSignatureForSelector:aSelector];
}

  4.若前三步均没有处理,则抛出异常doesNotRecognizeSelector:

NSInvocation

  对方法的另一种封装。相对于performSelector:withObject:只能传2参数的弊端,invocation类可以设置封装多参数

  a.对选择子签名  

    NSMethodSignature *instanceSignature = [self instanceMethodSignatureWithSelector:@selector(run:)];//实例方法签名

    NSMethodSignature *classSignature = [self methodSignatureWithSelector:@selector(run:)];//类方法签名

  b.实例化invocation,并设置参数

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
//设置方法调用者
invocation.target = self;
//注意:这里的方法名一定要与方法签名类中的方法一致
invocation.selector = @selector(run:);
NSString *way = @"byCar";
//这里的Index要从2开始,以为0跟1已经被占据了,分别是self(target),selector(_cmd)
[invocation setArgument:&way atIndex:2];
//3、调用invoke方法
[invocation invoke];

  c.获取invocation返回值

id res = nil;
if (signature.methodReturnLength != 0) {//有返回值
//将返回值赋值给res
[invocation getReturnValue:&res];
}
return res;

IMP指针

  指向选择子方法实现的指针,直接调用它相对于调用方法,会提高程序运行效率。

  相对于Method的方法交换,method_exchangeImplementations(method1,method2),使用IMP指针可以简化其对方法的额外实现,显得更加优雅。

  如果想IMP指针带参数或者返回值,需要将proprecessing:enable strict checking of objc_msgSend calls 配置为NO,默认是YES,表示IMP无参无返回值。

typedef id (*_IMP)(id,SEL,...);
typedef void (*_VIMP)(id,SEL,...); + (void)load {
static dispatch_once onceToken;
dispatch_once(&onceToken,^{
Method viewDidLoad = class_getInstanceMethod(self,@selector(viewDidLoad));
_IMP viewDidLoad_IMP = (_IMP)method_getImplementation(viewDidLoad);
method_setImplementation(viewDidLoad,imp_implementationWithBlock(^(id target,SEL action){
viewDidLoad_IMP(target,@selector(viewDidLoad));
// 新增代码 do extra things
}));
});
}

最后贴一张class的内部结构图,容以后细细研究

runtime使用总结的更多相关文章

  1. runtime梳理。

    一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C语言,函数的调用在编译的时候会决定调用哪个函数. 对于OC的函数,属于 ...

  2. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  3. Objective-C runtime初识

    Objective-C Runtime Describes the macOS Objective-C runtime library support functions and data struc ...

  4. Objective-C runtime的常见应用

    用Objective-C等面向对象语言编程时,"对象"(object)就是"基本构造单元"(building block).开发者可以通过对象来存储并传递数据. ...

  5. Runtime应用防止按钮连续点击 (转)

    好久之前就看到过使用Runtime解决按钮的连续点击的问题,一直觉得没啥好记录的.刚好今天旁边同时碰到这个问题,看他们好捉急而且好像很难处理,于是我先自己看看… 前面自己也学习了很多Runtime的东 ...

  6. iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条

    网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...

  7. VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误

    错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...

  8. DirectX runtime

    DirectX 9.0 runtime etc https://www.microsoft.com/en-us/download/details.aspx?id=7087 DirectX 11 run ...

  9. runtime

    7.runtime实现的机制是什么,怎么用,一般用于干嘛. 你还能记得你所使用的相关的头文件或者某些方法的名称吗? 运行时机制,runtime库里面包含了跟类.成员变量.方法相关的API,比如获取类里 ...

  10. runtime 第四部分method swizzling

    接上一篇 http://www.cnblogs.com/ddavidXu/p/5924597.html 转载来源http://www.jianshu.com/p/6b905584f536 http:/ ...

随机推荐

  1. JVM-垃圾收集算法基础

    目录 目录 前言 手动释放内存导致的问题 垃圾判定方法 哪些对象是垃圾? 引用计数算法 可达性分析法 垃圾收集算法 标记-清除 优点 缺点 优化 标记-复制 优点 缺点 优化 标记-整理 优点 缺点 ...

  2. Jenkins+gitlab发布Django程序

    Jenkins+gitlab发布Django程序 一. 二. 三.shell # !/bin/bash cd /root/upload_file #git add . #git commit -m ' ...

  3. Go语言的函数05---匿名函数

    package main import ( "fmt" "time" ) //延时执行一个匿名函数 func main071() { fmt.Println(& ...

  4. 永远的ace 实验七 团队作业4—团队项目需求建模与系统设计(1)

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST/ 这个作业要求链接 https://www.cnblogs.com/nwnu-da ...

  5. 用TVM在硬件平台上部署深度学习工作负载的端到端 IR 堆栈

    用TVM在硬件平台上部署深度学习工作负载的端到端 IR 堆栈 深度学习已变得无处不在,不可或缺.这场革命的一部分是由可扩展的深度学习系统推动的,如滕索弗洛.MXNet.咖啡和皮托奇.大多数现有系统针对 ...

  6. TensorRT-安装-使用

    TensorRT-安装-使用 一.安装 这里 是英伟达提供的安装指导,如果有仔细认真看官方指导,基本上按照官方的指导肯定能安装成功. 问题是肯定有很多人不愿意认真看英文指导,比如说我就是,我看那个指导 ...

  7. GPU编程和流式多处理器(二)

    GPU编程和流式多处理器(二) 2. 整数支持 SM具有32位整数运算的完整补充. 加法运算符的可选否定加法 乘法与乘法加法 整数除法 逻辑运算 条件码操作 to/from浮点转换 其它操作(例如,S ...

  8. 关于Numba的线程实现的说明

    关于Numba的线程实现的说明 由Numbaparallel目标执行的工作由Numba线程层执行.实际上,"线程层"是Numba内置库,可以执行所需的并发执行.在撰写本文时,有三个 ...

  9. CodeGen CreateFile实用程序

    CodeGen CreateFile实用程序 CreateFile实用程序允许根据存储库文件或结构定义创建ISAM文件. CreateFile实用程序的命令行选项如下: CreateFile -f & ...

  10. 从C到C++过渡的3个原因

    从C到C++过渡的3个原因 3 reasons to transition from C to C++ 几十年来,嵌入式软件工程师们一直在争论他们是否应该使用C或C++.根据2019年嵌入式市场调查, ...