In-depth study of Objective-c's NSObject

Source: Internet
Author: User



Let's take a look at the main definitions of NSObject as follows (including some of my understanding comments):


@interface NSObject <NSObject> {
    Class isa OBJC_ISA_AVAILABILITY;
}

/ * Initializing a Class * /

/ **
 * @author Arbboter, 15-01-15 22:01:47
 *
 * This method will only be called if the current class or category implements the load method, otherwise the method of the parent class will be called.
 * When is this method called: when the class or its class is statically linked or dynamically loaded
 * Therefore, when importing the header file of the class, the load method is called
 * /
+ (void) load;

/ **
 * @author Arbboter, 15-01-15 22:01:37
 *
 * Class methods are first called at runtime (instance methods or class methods, not including load methods)
 * This method is called before, and it shows that the calling hierarchy is from the parent class to the child class (this is consistent with the initialization of the object).
 * If the subclass does not implement this method, it will directly call the parent class. Note that if there is an inheritance hierarchy:
 * class_1 <-class_2 <-class_3
 * Only class_1 implements this method, then this method will be called when creating a clsaa_3 object
 * Three times (this method will be called for each layer of class), and all are methods implemented by class_1
 * /
+ (void) initialize;

/ * Creating, Copying, and Deallocating Objects * /

/ **
 * @author Arbboter, 15-01-15 22:01:57
 * Memory required to create an instance variable of the current class
 * @param zone has been ignored
 * @return object
 * /
+ (instancetype) allocWithZone: (struct _NSZone *) zone;

/ **
 * @author Arbboter, 15-01-15 23:01:06
 * Due to historical issues, this method will call the allocWithZone method to create object memory
 * @return object with memory allocated
 * /
+ (instancetype) alloc;

/ **
 * The isa of the initialization object describes the data structure of the class, and the other instance variables are 0.
 * /
-(instancetype) init;

/ **
 * @author Arbboter, 15-01-15 23:01:57
 * Equivalent to [[class name alloc] init]
 * @return initialized object
 * /
+ (instancetype) new;

/ **
 * @author Arbboter, 15-01-15 23:01:49
 * The object returned by the copyWithZone: method defined by the NSCopying protocol
 * @return copy object
 * /
-(id) copy;

/ **
 * @author Arbboter, 15-01-15 23:01:49
 * Object returned by mutableCopyWithZone: method defined by NSMutableCopying protocol
 * @return copy object
 * /
-(id) mutableCopy;

/ ** copy protocol * /
+ (id) copyWithZone: (struct _NSZone *) zone OBJC_ARC_UNAVAILABLE;
+ (id) mutableCopyWithZone: (struct _NSZone *) zone OBJC_ARC_UNAVAILABLE;

/ ** Release destruction object * /
-(void) dealloc;

/ ** Other methods are not discussed here * /

@end





The first two methods to be aware of are the call order and timing of load and initialize . Load is called when the class's header file or its extension is import, regardless of whether the class is used, and initialize is called before the method of the class is called the first time. More details are annoying, and the comments in the code are more detailed than the two.






Let's take a look at NSObject's unique member variable, ISA, which is of type class. By looking at the definition of class and related type definitions, you will find a very interesting fact as follows:


/ ** Definition of Objective-C objects * /
@interface NSObject <NSObject>
{
     / ** A pointer to the class where the instance of the current class is located * /
     Class isa OBJC_ISA_AVAILABILITY;
}

/ * Definition of some methods * /

@end

/ ** Objective-C object structure definition
  * Used to save the state of the object (instance variables)
  * /
struct objc_object
{
     Class isa OBJC_ISA_AVAILABILITY;
};

/ ** Class definition of Objective-C objects
  * Method for saving objects
  * /
struct objc_class
{
     Class isa OBJC_ISA_AVAILABILITY;
     / ** The definition of oc1.0 is omitted below * /
} OBJC2_UNAVAILABLE;

typedef struct objc_class * Class; // pointer to class structure
typedef struct objc_object * id; // pointer to Objective-C object structure
From the definition above we found the definition of the class and the familiar ID we mentioned earlier, and found a strange phenomenon. The member variables of the struct objc_object, Objc_class, and nsobject are actually identical
Class isa  OBJC_ISA_AVAILABILITY;


What is this for? You can look at the article "Objective-c's object Model analysis" and then look back at the question to see something. In fact, Nsobeject is the base class for almost all objects of objective-c, which defines the state information (variables) and methods (class methods and instance methods) of the class, and the class is a pointer to the method information that holds the nsobeject. The ID is used to hold a pointer to the state information of the NSObject, and the two collaborate together to better describe the nsobject. The member variable for each Objective-c object Isa points to the class information of this object, while Ismemberofclass: The method is judged by the ISA information.






Through analysis, we can find that the class is actually an object. When we define a class, we actually define two classes, a class is a class we can see, a class is a meta-class of the same name that we don't see, and the class we see is the object of that meta-class. (See the previous article for details)






(No train of thought, clear up again to add)







In-depth study of Objective-c's NSObject


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.