编程知识 cdmana.com

IOS learning -- detailed explanation of nsobject

One 、 Preface

The government is concerned about NSObject Is explained as follows : The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

The main idea is : This class is mostly said to be Objective-C Class base class , Provides a basic interface for subclasses to access the runtime system , And make subclasses have Objective-C The basic ability of the object .

Two 、NSObject.h Methods classification

1. Loading and initialization methods 2. Message sending method 3. Replication method 4. Judgment method 5. Memory management methods

2.1、 Loading and initialization methods

/**  The method is called by loading the class or classification at run time , 
 Each class is called only once  */
+ (void)load;

/**  Class instantiation needs to be initialized before use ,
  One class call , 
 If the subclass does not implement the method, the parent method is called  */
+ (void)initialize;

/**  Initialize object  */
- (instancetype)init
#if NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER
NS_DESIGNATED_INITIALIZER
#endif
;
 
/**  Allocate memory space for new objects and initialize ,
  be equal to [[NSObject alloc] init] */
+ (instancetype)new OBJC_SWIFT_UNAVAILABLE("use object initializers instead");

/**  Allocate memory space for new objects ,  Parameter passing nil */
+ (instancetype)allocWithZone:(struct _NSZone *)zone OBJC_SWIFT_UNAVAILABLE("use object initializers instead");

/**  Allocate memory space for new objects  */
+ (instancetype)alloc OBJC_SWIFT_UNAVAILABLE("use object initializers instead");

2.2、 Message sending method

/**  Send the specified message to the object , 
 Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector;

/**  Send a message with a parameter to the object , 
 Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector withObject:(id)object;

/**  Send a message with two parameters to the object , 
 Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

/**  Determine whether the instance can call the given method  */
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;

/**  Determine whether the class complies with the given protocol  */
+ (BOOL)conformsToProtocol:(Protocol *)protocol;

/**  Get point method implementation IMP The pointer to  */
- (IMP)methodForSelector:(SEL)aSelector;

/**  Get the implementation of the method pointing to the instance IMP The pointer to  */
+ (IMP)instanceMethodForSelector:(SEL)aSelector;

/**  If the function implementation cannot be found, this method will be called to throw an exception  */
- (void)doesNotRecognizeSelector:(SEL)aSelector;
 
/**  Returns the object to which the message was first forwarded , 
 The object was not found SEL Of IML When the method is called  */
- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

/** methodSignatureForSelector: Return not to nil Call the method , 
 You can override this method to SEL Forward to another object  */
- (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE("");

/**  Get method signature , 
 The object was not found SEL Of IML When the method is called , 
 You can override this method to throw out the signature of a function ,
 Again by forwardInvocation: To carry out  */
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");
 
/**  Get instance method signature  */
+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");

2.3、 Replication method

/**  Copy as immutable object  */
- (id)copy;
/**  Copy as mutable object  */
- (id)mutableCopy;

2.4、 Judgment method

/**  Whether the given object is a subclass or not  */
- (BOOL)isKindOfClass:(Class)aClass;

/**  Determine whether an object is an instance of a given class  */
- (BOOL)isMemberOfClass:(Class)aClass;

/**  Determine whether the object complies with the given protocol  */
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
 
/**  Determine whether an object can call a given method  */
- (BOOL)respondsToSelector:(SEL)aSelector;

2.5、 Memory management methods

/**  Object reference count plus 1,  stay MRC Next use  */
- (instancetype)retain OBJC_ARC_UNAVAILABLE;

/**  Object reference count minus 1,  stay MRC Next use  */
- (oneway void)release OBJC_ARC_UNAVAILABLE;

/**  The object reference count is automatically decremented in a deferred manner 1,  stay MRC Next use  */
- (instancetype)autorelease OBJC_ARC_UNAVAILABLE;

/**  Gets the object reference count ,  stay MRC Next use  */
- (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;

3、 ... and 、 Source code

#ifndef _OBJC_NSOBJECT_H_
#define _OBJC_NSOBJECT_H_
 
#if __OBJC__
 
#include <objc/objc.h>
#include <objc/NSObjCRuntime.h>
 
@class NSString, NSMethodSignature, NSInvocation;
 
#pragma mark -  The partial 
 
@protocol NSObject
 
/**  Determine whether two objects are equal ,  If equal, return YES,  Otherwise return to NO */
- (BOOL)isEqual:(id)object;
/**  Get objects hash value ,  Two objects are equal hash Same value  */
@property (readonly) NSUInteger hash;
 
/**  Get the parent class of the object  */
@property (readonly) Class superclass;
/**  Get the class of the current object  */
- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'type(of: anObject)' instead");
/**  Get the current object  */
- (instancetype)self;
 
/**  Send the specified message to the object ,  Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector;
/**  Send a message with a parameter to the object ,  Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector withObject:(id)object;
/**  Send a message with two parameters to the object ,  Return message execution result ( Equivalent to a method call ) */
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
 
/**  Determine whether the object inherits NSObject */
- (BOOL)isProxy;
 
/**  Whether the given object is a subclass or not  */
- (BOOL)isKindOfClass:(Class)aClass;
/**  Determine whether an object is an instance of a given class  */
- (BOOL)isMemberOfClass:(Class)aClass;
/**  Determine whether the object complies with the given protocol  */
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
 
/**  Determine whether an object can call a given method  */
- (BOOL)respondsToSelector:(SEL)aSelector;
 
/**  Object reference count plus 1,  stay MRC Next use  */
- (instancetype)retain OBJC_ARC_UNAVAILABLE;
/**  Object reference count minus 1,  stay MRC Next use  */
- (oneway void)release OBJC_ARC_UNAVAILABLE;
/**  The object reference count is automatically decremented in a deferred manner 1,  stay MRC Next use  */
- (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
/**  Gets the object reference count ,  stay MRC Next use  */
- (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;
/**  Get object storage space ,  stay MRC Next use  */
- (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
 
/**  Get object description information  */
@property (readonly, copy) NSString *description;
@optional
/**  Get the description of the object in the debugger  */
@property (readonly, copy) NSString *debugDescription;
 
@end
 
#pragma mark -  Class section 
 
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0)
OBJC_ROOT_CLASS
OBJC_EXPORT
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
 
/**  The method is called by loading the class or classification at run time ,  Each class is called only once  */
+ (void)load;
/**  Class instantiation needs to be initialized before use ,  One class call ,  If the subclass does not implement the method, the parent method is called  */
+ (void)initialize;
/**  Initialize object  */
- (instancetype)init
#if NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER
NS_DESIGNATED_INITIALIZER
#endif
;
 
/**  Allocate memory space for new objects and initialize ,  be equal to [[NSObject alloc] init] */
+ (instancetype)new OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
/**  Allocate memory space for new objects ,  Parameter passing nil */
+ (instancetype)allocWithZone:(struct _NSZone *)zone OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
/**  Allocate memory space for new objects  */
+ (instancetype)alloc OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
/**  Release object ,  When the reference count of an object is 0 Will call this method  */
- (void)dealloc OBJC_SWIFT_UNAVAILABLE("use 'deinit' to define a de-initializer");
/**  The garbage collector processes the memory it uses before calling this method . */
- (void)finalize OBJC_DEPRECATED("Objective-C garbage collection is no longer supported");
 
/**  Copy as immutable object  */
- (id)copy;
/**  Copy as mutable object  */
- (id)mutableCopy;
 
/**  Copy as immutable object on the specified memory space ,  stay MRC Next use  */
+ (id)copyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
/**  Copy as mutable object on specified memory space ,  stay MRC Next use  */
+ (id)mutableCopyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
 
/**  Determine whether the instance can call the given method  */
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
/**  Determine whether the class complies with the given protocol  */
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
/**  Get point method implementation IMP The pointer to  */
- (IMP)methodForSelector:(SEL)aSelector;
/**  Get the implementation of the method pointing to the instance IMP The pointer to  */
+ (IMP)instanceMethodForSelector:(SEL)aSelector;
/**  If the function implementation cannot be found, this method will be called to throw an exception  */
- (void)doesNotRecognizeSelector:(SEL)aSelector;
 
/**  Returns the object to which the message was first forwarded ,  The object was not found SEL Of IML When the method is called  */
- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
/** methodSignatureForSelector: Return not to nil Call the method ,  You can override this method to SEL Forward to another object  */
- (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE("");
/**  Get method signature ,  The object was not found SEL Of IML When the method is called ,  You can override this method to throw out the signature of a function , Again by forwardInvocation: To carry out  */
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");
 
/**  Get instance method signature  */
+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");
 
/**  Allow weak references to scalars ,  For all allowsWeakReference Method returns NO You can't use __weak Modifier  */
- (BOOL)allowsWeakReference UNAVAILABLE_ATTRIBUTE;
/**  Keep weak reference variables ,  In the use of __weak Modifier variables ,  When the assigned object is retainWeakReference Method returns NO Under the circumstances ,  This variable will use “nil” */
- (BOOL)retainWeakReference UNAVAILABLE_ATTRIBUTE;
 
/**  Determine whether it is a subclass of another class  */
+ (BOOL)isSubclassOfClass:(Class)aClass;
 
/**  Dynamically parse a class method  */
+ (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
/**  Dynamically parse an instance method ,  The object was not found SEL Of IML When the method is called ,  You can override this method to add the required SEL */
+ (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
 
/**  Get objects hash value ,  Two objects are equal hash Same value */
+ (NSUInteger)hash;
/**  Get the parent class of the object  */
+ (Class)superclass;
/**  Get class  */
+ (Class)class OBJC_SWIFT_UNAVAILABLE("use 'aClass.self' instead");
/**  Get object description information  */
+ (NSString *)description;
/**  Get the description of the object in the debugger  */
+ (NSString *)debugDescription;
 
@end
 
#endif
 
#endif

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[mukekeheart]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225111921871v.html

Scroll to Top