class和superclass源碼(runtime源碼里有)
Foundation框架里NSObject框架里的有些基礎(chǔ)方法是開(kāi)源的授账,
objc4源碼鏈接 https://opensource.apple.com/tarballs/objc4/
在源碼里搜索NSOject.mm文件橘沥,然后搜索class就找到源碼了
class的作用是獲取對(duì)象的類(lèi)對(duì)象炮赦,superclass的作用是獲取父類(lèi)碳锈。
源碼
+ (Class)class {
return self;
}
- (Class)class {
return object_getClass(self);
}
+ (Class)superclass {
return self->superclass;
}
- (Class)superclass {
return [self class]->superclass;
}
isMemberOfClass
左邊對(duì)象的類(lèi)對(duì)象是否等于右邊的類(lèi)對(duì)象
isKindOfClass
左邊的類(lèi)對(duì)象是否是等于右邊類(lèi)對(duì)象旗笔,或者是右邊類(lèi)對(duì)象的子類(lèi)
源碼
重點(diǎn)
看調(diào)用方法的是實(shí)例對(duì)象還是類(lèi)對(duì)象尊浓,或者說(shuō)看是類(lèi)方法還是對(duì)象方法过吻,類(lèi)方法調(diào)用的話电抚,object_getClass((id)self)惕稻,這行代碼得到的是元類(lèi)對(duì)象,那右邊傳入的類(lèi)也應(yīng)該是元類(lèi)對(duì)象才會(huì)相等蝙叛。右邊傳入的如果是NSObject俺祠,只要左邊是NSObject的子類(lèi),一般是成立的甥温。
+ (BOOL)isMemberOfClass:(Class)c ls {
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;
}
+ (BOOL)isSubclassOfClass:(Class)cls {
for (Class tcls = self; tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}