NSObject定义

NSObject可以说是OC类的万物之源了,今天我们就来好好看看他的定义.

/* NSObject.h
Copyright (c) 1994-2012, Apple Inc. All rights reserved.
*/

ifndef OBJC_NSOBJECT_H

define OBJC_NSOBJECT_H

if OBJC

include

include

@class NSString, NSMethodSignature, NSInvocation;

//定义了一个名为NSObject的协议.
@protocol NSObject

  • (BOOL)isEqual:(id)object;
    //这个方法很常见,我们经常用来比较一个对象是否相同或者值是否相等.相同返回yes.而它是通过什么比较的呢?就是下面的hash值.
    @property (readonly) NSUInteger hash;

@property (readonly) Class superclass;//父类.

  • (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead");//不知道干啥用的.
  • (instancetype)self; //返回自己的对象.

//执行一个包装方法.与普通相比,它可以执行到runtime阶段时候才有的方法.下面的不带参数,带一个参数,带两个参数.

  • (id)performSelector:(SEL)aSelector;
  • (id)performSelector:(SEL)aSelector withObject:(id)object;
  • (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

//是否从NSObject继承下来的.

  • (BOOL)isProxy;
    //是不是某个类或者父类的实例
  • (BOOL)isKindOfClass:(Class)aClass;
    //是不是某个类的实例.
  • (BOOL)isMemberOfClass:(Class)aClass;
    //是不是遵守某个协议.
  • (BOOL)conformsToProtocol:(Protocol *)aProtocol;
    //是不是接受者实现或者继承了某个方法可以响应这个消息.
  • (BOOL)respondsToSelector:(SEL)aSelector;

//引用计数器加一

  • (instancetype)retain OBJC_ARC_UNAVAILABLE;
    //引用计数器减一
  • (oneway void)release OBJC_ARC_UNAVAILABLE;
    //当当前自动释放池释放时释放.
  • (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
    //返回接收对象的引用计数器.不是很准,因为自动释放池可能会推迟释放.
  • (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;
    //返回一段结构体--对象被分配的区域,价值不大.
  • (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;

//接受者的描述.%@ 打印的东西
@property (readonly, copy) NSString *description;
@optional
//同上,不过是在Debug模式下的.
@property (readonly, copy) NSString *debugDescription;

@end

OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0)
OBJC_ROOT_CLASS
OBJC_EXPORT
//正式开始定义NSObject了.
@interface NSObject {
//类指针.
Class isa OBJC_ISA_AVAILABILITY;
}
//触发,当类或者分类加入Runtimel的时候.

  • (void)load;
    //是类接受到的第一个消息.
  • (void)initialize;
    //初始化
  • (instancetype)init

if NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER

NS_DESIGNATED_INITIALIZER

endif

;

//不知道

  • (instancetype)new OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
    //历史遗留方法初始分配内存.
  • (instancetype)allocWithZone:(struct _NSZone *)zone OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
    //..
  • (instancetype)alloc OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
  • (void)dealloc OBJC_SWIFT_UNAVAILABLE("use 'deinit' to define a de-initializer");
    //有垃圾回收机制时候代替dealloc.
  • (void)finalize OBJC_DEPRECATED("Objective-C garbage collection is no longer supported");
    //返回调用copyWithZone的结果
  • (id)copy;
    //返回调用mutableCopyWithZone的结果
  • (id)mutableCopy;
    //返回接受者.协议不同
  • (id)copyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
    //返回接受者.协议不同.
  • (id)mutableCopyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
    //实例化对象是否响应给定的方法
  • (BOOL)instancesRespondToSelector:(SEL)aSelector;
    //是否遵守协议.
  • (BOOL)conformsToProtocol:(Protocol *)protocol;
    //找出接受者实现方法地址.
  • (IMP)methodForSelector:(SEL)aSelector;
    //同上,不过是类方法.
  • (IMP)instanceMethodForSelector:(SEL)aSelector;
    //runtime不能找不到方法时访问
  • (void)doesNotRecognizeSelector:(SEL)aSelector;
    //重定向
  • (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
  • (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE("");
    //返回一个对象包括方法描述的定义.
  • (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");
  • (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");
    //是否允许弱引用
  • (BOOL)allowsWeakReference UNAVAILABLE_ATTRIBUTE;
    //是否增加弱引用
  • (BOOL)retainWeakReference UNAVAILABLE_ATTRIBUTE;
    //是否是给定类的子类或者是相同的.
  • (BOOL)isSubclassOfClass:(Class)aClass;
    //动态的提供一个方法的实现.
  • (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
  • (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
    //获得当前hash值
  • (NSUInteger)hash;
    //获取父类
  • (Class)superclass;
  • (Class)class OBJC_SWIFT_UNAVAILABLE("use 'aClass.self' instead");
    //描述
  • (NSString *)description;
    //Debug模式下的描述.
  • (NSString *)debugDescription;

@end

endif

endif

你可能感兴趣的:(NSObject定义)