美文网首页
runtime学习

runtime学习

作者: 06f43b0b52f7 | 来源:发表于2018-07-31 17:28 被阅读7次

美团技术团队iOS学习:https://tech.meituan.com/DiveIntoCategory.html

runtime学习网址:https://www.cnblogs.com/ioshe/p/5489086.html

首先从runtime的数据结构

1 SEL

  • typedef struct objc_Selector *SEL

2 id

  • typedef struct objc_object *id
  • typedef struct objc_object{ class isa}
  • 以上定义,看到 Objc_object 结构体包含一个isa指针,根据isa指针就可以找到对象所属的类

Class

  • typedef struct objc_class *class;
Class super_class
const char *name
long version
long info
long instance_size
struct objc_ivar_list   *ivars
struct objc_methodlist  *methodList
struct  objc_cache *cache
struct objc_protocol_list *protocols

objc_ivar_list结构体用来存储成员变量的列表 ,而objc_ivar 则是存储了单个成员变量的信息;同理objc_method_list结构体存储着方法数组的列表,而单个方法的信息则由objc_method结构体存储

objc_method 存储了方法名,方法类型,方法实现

  • 方法名类型为 SEL
  • 方法类型 method_types 是个char指针 ,存储方法的参数类型和返回值类型
  • method_imp 指向了方法的实现,本质是一个函数指针

Ivar

  • Ivar表示成员变量的类型
typedef struct objc_ivar *Ivar;

struct  objc_ivar {
 char *ivar_name
char *ivar_type
int  ivar_offset//是基地址偏移字节

}

IMP

IMP在objc.h中的定义是:
typedef id(*IMP)(id,SEL,...);
它是一个函数指针

运行时学习

  • class_copyPropertyList和class_copyIvarList的区别
  1. 转自:https://blog.csdn.net/gx_wqm/article/details/51443267

内容

  • class_copyPropertyList:

通过类名获得类的属性变量。

  • class_copyIvarList

通过类名获得类的实例变量。

  • 也就是说:

class_copyPropertyList获得的是由@property修饰过的变量,

class_copyIvarList获得的是class_copyPropertyList修饰的以及在m文件的中@implementation内定义的变量

eg:

//.h


@interface Model : NSObject



@property (nonatomic,copy) NSString *sex;

@property (nonatomic,copy) NSString *name;

@property (nonatomic,assign) NSInteger age;

@property (nonatomic,strong) NSDictionary *imgCode;



@end
//.m

@interface Model ()

@property (nonatomic,copy) NSString *Id;

@end



@implementation Model {

    NSInteger _index;

}



@end

相关文章

网友评论

      本文标题:runtime学习

      本文链接:https://www.haomeiwen.com/subject/hgihvftx.html