iOS runtime初探和总结

内存分布

程序运行内存分为5大部分 堆、栈、初始化变量区、未初始化变量区、代码区
栈:存放指针地址
堆:存放真实的对象
查找过程: 先通过栈区找到指针 再通过指针找到堆上真实的对象

类对象、实例对象、元类
类在runtime中的样子 是一个类的结构体
struct objc_class {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
    Class _Nullable super_class     父类;
    const char * _Nonnull name                               类名;
    long version                                             版本;
    long info                                        类信息;
    long instance_size                      大小;
    struct objc_ivar_list * _Nullable ivars     成员变量链表;
    struct objc_method_list * _Nullable * _Nullable methodLists     方法列表;
    struct objc_cache * _Nonnull cache 缓存 如果方法已经调用过 会先从缓存中找;
    struct objc_protocol_list * _Nullable protocols 协议列表;
#endif

} OBJC2_UNAVAILABLE;
objc_object 是一个类的实例的结构体
struct objc_object {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY; 指向类
};
/// A pointer to an instance of a class.
typedef struct objc_object *id; 

从上边代码看 id也是一个objc_object结构体指针 也是一个实例 所以他可以指向任意对象。
元类:因为类也是一个对象,所以apple引出了元类的概念,类的isa指针指向元类,类的实例的isa指向类 所以向一个类实例发送消息时会向类的方法列表中查找,接着找父类,向一个类发送消息时 会从元类的方法列表中查找。

isa 走向图 类实例isa->类 类的isa->元类 元类isa->跟元类 跟元类isa->自己形成一个闭环。

runtime常用api介绍
// runtime api
    const char *className = class_getName([People class]);
    NSLog(@"--className---%s",className);
    const char *superClass = class_getName(class_getSuperclass([People class]));
    NSLog(@"--superClass---%s",superClass);
    NSLog(@"---size--%zu",class_getInstanceSize([People class]));
    // 获取变量列表
    unsigned int ivarsCount = 0;
    Ivar *ivars = class_copyIvarList([People class], &ivarsCount);
    for (int i = 0; i < ivarsCount; i ++){
        NSLog(@"---ivars----%s",ivar_getName(ivars[i]));
    }
    // 获取方法列表
    unsigned int methodsCount = 0;
    Method *methods = class_copyMethodList([People class], &methodsCount);
    for (int i = 0; i < methodsCount; i ++){
        NSLog(@"---methods----%s",method_getName(methods[i]));
    }
    unsigned int properties = 0;
    //获取属性列表
    objc_property_t *properties_t = class_copyPropertyList([People class], &properties);
    for (int i = 0; i < properties; i ++){
        NSLog(@"---properties----%s",property_getName(properties_t[i]));
    }
    // 动态创建类
    Class son = objc_allocateClassPair([People class], @"Son", 0);
    class_addMethod(son, @selector(method1), (IMP)sonMethod1, "v@:");
    objc_registerClassPair(son);// 注册这个类 一定要写
    id instance = [[son alloc]init];
    [instance performSelector:@selector(method1)];
    void sonMethod1(){
    printf("sonMethod1--执行了 哈哈哈---");
}
结果:
XingNengTest[49761:4868510] --className---People
2019-11-09 16:37:52.175633+0800 [49761:4868510] --superClass---NSObject
2019-11-09 16:37:52.175714+0800 [49761:4868510] ---size--16
2019-11-09 16:37:52.175794+0800 [49761:4868510] ---ivars----_name
2019-11-09 16:37:52.175869+0800 [49761:4868510] ---methods----init
2019-11-09 16:37:52.175947+0800 [49761:4868510] ---methods----.cxx_destruct
2019-11-09 16:37:52.176065+0800 [49761:4868510] ---methods----name
2019-11-09 16:37:52.176227+0800 [49761:4868510] ---methods----setName:
2019-11-09 16:37:52.176389+0800 [49761:4868510] ---properties----name
sonMethod1--执行了 哈哈哈---
还有很多 不写了
方法调用流程和消息转发

消息函数,Objc中发送消息是用中括号把接收者和消息括起来,只到运行时才会把消息和方法实现绑定。
SEL 只是一个方法的指针 IMP 是这个方法的具体实现
方法调用流程:当一个消息发送给对象时 objc_msgSend会根据isa指针找到类的方法列表 从中查找 如果找不到就找父类 直到找打rootClass 这个过程有一个找到了就可以执行 如果找不到就会消息转发
分为三个步骤:

  1. 动态方法解析
第一次机会允许动态添加一个方法
// 动态方法解析
    People *p1 = [[People alloc]init];
    [p1 performSelector:@selector(doMethod)];
People.m
+ (BOOL)resolveInstanceMethod:(SEL)sel{
    if ([NSStringFromSelector(sel) isEqualToString:@"doMethod"]){
        class_addMethod([self class], @selector(doMethod), (IMP)handleException, "v@:");
    }
    return [super resolveInstanceMethod:sel];
}
void handleException(){
    printf("-------\n方法没有 但是我给他加了");
}
  1. 重定向接受着
第二次机会提供一个新的接收者
调用还一样
People *p1 = [[People alloc]init];
[p1 performSelector:@selector(doMethod)];
People.m
//+ (BOOL)resolveInstanceMethod:(SEL)sel{
//    if ([NSStringFromSelector(sel) isEqualToString:@"doMethod"]){
//        class_addMethod([self class], @selector(doMethod), (IMP)handleException, "v@:");
//    }
//    return [super resolveInstanceMethod:sel];
//}
//void handleException(){
//    printf("-------\n方法没有 但是我给他加了");
//}
- (id)forwardingTargetForSelector:(SEL)aSelector{
    if ([NSStringFromSelector(aSelector) isEqualToString:@"doMethod"]){
        return [Man new];
    }
    return [super forwardingTargetForSelector:aSelector];
}
Man.m
- (void)doMethod{
    NSLog(@"----Man---重定向一个接受着");
}
执行结果
----Man---重定向一个接受着
  1. 消息转发
    forwardInvocation:方法只有在消息接收对象无法正常响应消息时才被调用。
  • (void)forwardInvocation:(NSInvocation *)anInvocation
    //必须重写这个方法,消息转发使用这个方法获得的信息创建NSInvocation对象。
  • (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
最终将执行的目标转到 [[Man alloc]init]
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    NSLog(@"---methodSignature--%@",anInvocation.methodSignature);
    [anInvocation invokeWithTarget:[[Man alloc]init]];
}
// 返回未处理的方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    if (aSelector == @selector(doMethod)){
        NSMethodSignature *singnature = [NSMethodSignature signatureWithObjCTypes:"v@:"];
        return singnature;
    }
    return [super methodSignatureForSelector:aSelector];
}

和重定向接受着的区别是消息转发可以转给多个接收者。

总结

runtime 还有很多需要研究的 自己也看了不少,今天先把一部分总结写了,以后还会继续写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值