Objective-c source Code (a): Object model

Source: Internet
Author: User

First, you need to figure out what the class is and what the object is. (To make this clear, let's start with a chestnut: examples of objects, such as: Human is a class; a person is an object; you are an instance; the instance itself is an object.) This basic can understand the difference, if still do not know, go to Google for a bit. )

Object

in Objective-c, each object is an instance of a class, and the ISA pointer to that object points to the class to which it belongs. Open the objc-private.h file, and in line 126th you see objective-c? ? ? ? object is defined as a struct objc_object , and theobject in Objective-c is essentially a struct object, where Isa is its only private variable.

Similarly, in lines 48th through 52nd, you can find the Class and ID type definitions, which is why the ID type can point to any object. Where objc_class is the definition of class in objective-c.

class

The class of the object not only describes the object's data: The object occupies memory size, the type layout of the member variable, but also describes the behavior of the object: the object can respond to the information, the implementation of the strength of the method (many of the runtime's explanation of the article, in fact, is in this piece of fuss) and so on. Therefore, when we call [receiver message] to send a message to an object, the object can respond to the message and need to find the class (including superclass) it belongs to by using ISA to know.

Open objc-runtime-new.h , you can find the definition of the struct objc_class in the objective-c in line 687. You can see that the Objective-c class is also a struct object and inherits the struct Objc_object .

< Span style= "LINE-HEIGHT:22.5PX; Background-color:rgb (255, 255, 255); " >     so: &NBSP; objective-c are essentially objects, and we call them class objects. As I said before, all objects are instances of a class, so what kind of class object is an instance of what class, this is a question worth thinking about.

< Span style= "LINE-HEIGHT:22.5PX; Background-color:rgb (255, 255, 255); " >objective-c has a very special nsobject, most of the classes inherit from it, it is  objective-c, the other is Nsproxy (another article has). Open NSObject.h, you can see that the NSObject class has only one member variable Isa, all inherited from the NSObject class will also have this member variable.

Meta class

In essence, The class in Objective-c is also an object, which is also an instance of a class, which we call a meta-class (Metaclass).

Therefore, we can invoke a class method, such as [NSObject New], to send a message to the class object. Similarly, the class object can respond to this message by finding the class (meta-Class) to which the class object belongs through ISA. That is, the instance method is saved in the class, and the class method is persisted in the Meta class.

A meta-class is also an object (a meta-class object), which is also an instance of a class, which we call the root meta-class (Root Metaclass ). The special is that all meta classes belong to the same root class (of course, the root class is also a meta-class, so it belongs to the class is also the root meta class, which itself). The root meta-class refers to the meta-class of the root class, specifically the meta-class of the root class NSObject.

On the classic chart: through the above analysis, and then look at this picture, much clearer.


Objective-c source Code (a): Object model

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.