isa走位图
在对象的本质这篇文章文章中,我们已经分析过isa的结构,在对象的isa中可以取出它所属的类。类的底层的结构是objc_class继承自objc_object,所以类也有isa,那么类的isa指向的是什么。
自定义类的isa走位
这里要引出两个概念,元类,他是由系统自动生成的,每个类对应一个元类,与类同名。元类的isa指向根元类。
直接取出
Person类的isa0x0000000100008268,使用mask取出指向的类0x0000000100008268,打印一下还是Person,但是地址已经不同了,这个就是Person的元类,发现一个问题,似乎类的isa与元类的地址相同,这里可以暂时认为类的isa就是元类的地址。获取元类的isa0x00007fff889bdfe0,取出指向的类为NSObject,但是他不是根类,而是根元类。
自定义类的isa走位
NSObject的isa探索
创建一个
NSObject对象obj,取出obj指向的类0x00007fff889be008,po一下是NSObject类。取出NSObject的isa,取出其指向的类0x00007fff889bdfe0,po一下还是NSObject类,但是这个和之前的地址是不一样的。从0x00007fff889bdfe0中取出isa,从isa中取出指向的类,发现还是0x00007fff889bdfe0。从这里我们可以得出结论:
NSObject类的isa走位
继承链与isa的关系之类的元类的父类与类的父类的元类
自定义一个Person类,自定义Student类继承自Person,取出Student的元类的父类,再取出Student的父类的元类也就是Person的元类对比,结果是相同的。
著名的isa走位图-->总结
iOS中各种类型的编码
NSLog(@"char --> %s",@encode(char));
NSLog(@"int --> %s",@encode(int));
NSLog(@"short --> %s",@encode(short));
NSLog(@"long --> %s",@encode(long));
NSLog(@"long long --> %s",@encode(long long));
NSLog(@"unsigned char --> %s",@encode(unsigned char));
NSLog(@"unsigned int --> %s",@encode(unsigned int));
NSLog(@"unsigned short --> %s",@encode(unsigned short));
NSLog(@"unsigned long --> %s",@encode(unsigned long long));
NSLog(@"float --> %s",@encode(float));
NSLog(@"bool --> %s",@encode(bool));
NSLog(@"void --> %s",@encode(void));
NSLog(@"char * --> %s",@encode(char *));
NSLog(@"id --> %s",@encode(id));
NSLog(@"Class --> %s",@encode(Class));
NSLog(@"SEL --> %s",@encode(SEL));
int array[] = {1,2,3};
NSLog(@"int[] --> %s",@encode(typeof(array)));
typedef struct person{
char *name;
int age;
}Person;
NSLog(@"struct --> %s",@encode(Person));
typedef union union_type{
char *name;
int a;
}Union;
NSLog(@"union --> %s",@encode(Union));
int a = 2;
int *b = {&a};
NSLog(@"int[] --> %s",@encode(typeof(b)));