Class and metadata Declaration
Translated fromClass and metaclassesOriginal non-nengong
The chaotic logic of ten egg and egg
Body
Objective-C is a class-based object system. every class is an instance of the class; the ISA pointer of the object points to the class to which it belongs. class describes the data of an object: the size of the object, the type of instance variables, and the layout form. class also defines the behavior of the object: The instance method implemented by the selector to respond.
The method list of a class is a cluster of instance methods, and the selector that the object can respond. when you send a message to the instance, The objc_msgsend () method list of the object's class (or parent class) determines which method to call.
Each class is also an object. it has an ISA pointer and other data, and can then respond to the selector. when you call a class method, like [nsobject alloc], you actually send a message to the class object.
Because a class is an object, it must be an instance of another class: A metadata class. A meta-class is a description of a class object, just like a class is a description of a common object. in general, the metadata method list is the class method: The Class Object selector can respond. when you send a message to a class (an instance of the Meta class)-objc_msgsend () to retrieve the list of methods of the Meta class (or its parent class), you decide which method to call. meta class describes class methods for class objects, just like class describes class methods for instance objects.
So what is a metadata? What has the metadata always been? No. A metadata class is an instance of the root class metadata class. the ISA chain is cyclical at last: The instance points to the class, the class points to the Meta class, the Meta class points to the root class, and the root class points to the instance itself. this kind of meta-class pointer behavior is extremely rare, so no one in the real world sends messages to meta-class objects.
More importantly, the parent class of the Meta class. the parent class chain of the meta-class is parallel to the parent class chain of the class, so the class method is inherited along with the instance method. at the same time, the parent class of the root element class is the root class, so each class Object responds to the instance method of the root class. finally, a class object is a root class instance, just like other objects.
Confused? This image may be useful. remember, when a message is sent to any object, this method starts to retrieve the ISA pointer of the object and then continues to retrieve the parent class chain. the instance method is defined by the class, and the class method is defined by adding the root class (not the Meta class) through the Meta class.
In strict computer linguistics, the formulation of a class and a meta-class can be any form, through deep-seated meta-class chains and many classes instantiated from a single Meta-class. objective-C uses meta-classes, and objective-C uses meta-classes for actual purposes, such as class methods. However, meta-classes are hidden at other times. for example, [nsobject class] is the same as [nsobject self], although the actual elements should return the nsobject-> Isa-pointed Meta class. objective-C language is a combination of actual trade-offs. It limits the class mode before obtaining more elements.