NSobject常用api

核心是类模型
1 isEqual
内部实现比较指针是否相等
if (self == other) return YES;
if (!self || !other) return NO;
2 performSelector
((id(*)(id, SEL))objc_msgSend)((id)self, sel);
3 isMemberOfClass

  • (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
    }
    4
  • (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
    if (tcls == cls) return YES;
    }
    return NO;
    }
    5
  • (BOOL)isSubclassOfClass:(Class)cls {
    for (Class tcls = self; tcls; tcls = tcls->superclass) {
    if (tcls == cls) return YES;
    }
    return NO;
    }
    6
  • (BOOL)conformsToProtocol:(Protocol *)protocol {
    if (!protocol) return NO;
    for (Class tcls = self; tcls; tcls = tcls->superclass) {
    if (class_conformsToProtocol(tcls, protocol)) return YES;
    }
    return NO;
    }
    7 这里验证了类模型的争取性
  • (BOOL)respondsToSelector:(SEL)sel {
    if (!sel) return NO;
    return class_respondsToSelector_inst(object_getClass(self), sel, self);
    }
  • (BOOL)respondsToSelector:(SEL)sel {
    if (!sel) return NO;
    return class_respondsToSelector_inst([self class], sel, self);
    }

你可能感兴趣的:(NSobject常用api)