+ (Class)class {
return self;
}
- (Class)class {
return object_getClass(self);
}
Class object_getClass(id obj)
{
if (obj) return obj->getIsa();
else return Nil;
}
inline Class
objc_object::getIsa()
{
if (isTaggedPointer()) {
uintptr_t slot = ((uintptr_t)this >> TAG_SLOT_SHIFT) & TAG_SLOT_MASK;
return objc_tag_classes[slot];
}
return ISA();
}
inline Class
objc_object::ISA()
{
assert(!isTaggedPointer());
return (Class)(isa.bits & ISA_MASK);
}
+ (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;
}
+ (BOOL)isMemberOfClass:(Class)cls {
return object_getClass((id)self) == cls;
}
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
總結
+ isKindOfClass 會拿到當前類對象的 isA(metaclass ) 遞歸(superclass)和當前比較是否相等
+ isMemberOfClass的源碼實現(xiàn)是拿到自己的isa指針和自己比較这溅,是否相等
- isKindOfClass 會拿到當前實例對象的 class 遞歸(superclass)的和當前類對象進行比較
- isMemberOfClass 會拿到當前實例對象的類對象 和當前 實例對象比較
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者