1. isa 的指向如图:
24165805-35d0f6252db8bf96.png2. 文字表述如下:
实例对象的isa 指向类对象
类对象的isa 指向元类
元类的isa指向 NSObjec 的元类
NSObject 元类指向自己
3.代码验证
定义一个 Person 类
@interface CCPerson : NSObjectend
@implementation CCPerson
@end
打印 isa 指向
CCPerson *p = [CCPerson alloc];
Class pClass = object_getClass(p);
Class pClass1 = object_getClass(pClass);
Class pClass2 = object_getClass(pClass1);
NSLog(@"person 类是 %p 元类%p object 元类 %p",pClass,pClass1,pClass2);
// NSObject实例对象
NSObject *object1 = [NSObject alloc];
// NSObject类
Class class = object_getClass(object1);
// NSObject元类
Class metaClass = object_getClass(class);
// NSObject根元类
Class rootMetaClass = object_getClass(metaClass);
// NSObject根根元类
Class rootRootMetaClass = object_getClass(rootMetaClass);
NSLog(@"\n%p 实例对象\n%p 类\n%p 元类\n%p 根元类\n%p 根根元类",object1,class,metaClass,rootMetaClass,rootRootMetaClass);
/**
Person 类是 0x100008108 元类0x1000080e0 object 元类 0x7fff991880f0
0x1005042a0 实例对象
0x7fff99188118 类
0x7fff991880f0 元类
0x7fff991880f0 根元类
0x7fff991880f0 根根元类
4.lldb 调试 验证
如下图,查看源码,获取如何获取isa ,
image.png
联合体中 通过 与 0x00007ffffffffff8 按为与获取
CCPerson *p = [CCPerson alloc];
查找CCPersion 的isa
x/4gx p
0x10201ce50: 0x001d800100008109 0x0000000000000000
0x10201ce60: 0x74786554534e5b2d 0x61427265646e6946
(lldb) p/x CCPerson.class
(Class) $7 = 0x0000000100008108 CCPerson
(lldb) p/x 0x001d800100008109&0x00007ffffffffff8
(long) $9 = 0x0000000100008108
继续查找
x/4gx 0x0000000100008108
0x100008108: 0x00000001000080e0 0x00007fff99188118
0x100008118: 0x0000000102304080 0x0001801000000003
(lldb) p/x 0x00000001000080e0&0x00007ffffffffff8
(long) $11 = 0x00000001000080e0
(lldb) po 0x00000001000080e0
CCPerson
x/4gx 0x00000001000080e0
0x1000080e0: 0x00007fff991880f0 0x00007fff991880f0
0x1000080f0: 0x00000001004715d0 0x0004e03100000007
(lldb) p/x 0x00007fff991880f0&0x00007ffffffffff8
(long) $13 = 0x00007fff991880f0
(lldb) po $13
NSObject
x/4gx $13
0x7fff991880f0: 0x00007fff991880f0 0x00007fff99188118
0x7fff99188100: 0x00000001006072b0 0x0004e03100000007
(lldb) p/x 0x00007fff991880f0&0x00007ffffffffff8
(long) $14 = 0x00007fff991880f0
(lldb) po $14
NSObject
x/4gx NSObject.class
0x7fff99188118: 0x00007fff991880f0 0x0000000000000000
0x7fff99188128: 0x00000001020231b0 0x0002801000000003
(lldb) p/x 0x00007fff991880f0&0x00007ffffffffff8
(long) $17 = 0x00007fff991880f0
*/
网友评论