1.官方图片:
2.几个概念:
a.oc中的对象就是objc_object的结构体
b.oc中的类就是objc_class的结构体, 而且objc_class继承objc_object,所以说oc中类也是对象
c. objc_object中都会有个isa指针,objc_object和对应isa地址存在一一对应关系(objc_object地址>>3就是isa的地址)
d. isMemberOfClass,isKindOfClass
+ (BOOL)isMemberOfClass:(Class)cls {
return object_getClass((id)self) == cls;
}
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
+ (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
- (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
object_getClass源码如下:
Class object_getClass(id obj)
{
if (obj) return obj->getIsa();
else return Nil;
}