美文网首页
NSobject常用api

NSobject常用api

作者: 事件_666 | 来源:发表于2019-07-05 14:23 被阅读0次

    核心是类模型
    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

          本文链接:https://www.haomeiwen.com/subject/cgyshctx.html