[Objc]: class and metadata

Source: Internet
Author: User

Original article:

Http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

Better translation link:

Http://blog.csdn.net/timothyfly/article/details/8042464

Objective-C language is a class-based object-oriented language. Each object is an instance of a class, and the ISA pointer of each object points to its class. This class describes the attributes of an object: the size of the allocated space, the type and layout of instance variables. This class also describes the object method: the response selection method and the instance method to be implemented.

The method list of a class is composed of a series of instance methods and the selection methods that the object needs to respond. When you send a message to an instance, the objc_msgsend () method traverses the list of methods of the object class (including the parent class, if any) to determine which method is called.

Each objective-C class is also an object. It also has an ISA pointer and other attributes, as well as a response selection method. When you call a "class method", such as [nsobject alloc], you are actually sending a message to a class object.

Because a class is an object, it must also be an instance of another class: We call this class as the object class as the Meta class. A meta-class is a description of a class object, just like a description of a class as a common instance object. In particular, the metadata method list is the selection method in the class method: The Class Object response. When you send a message to a class-an instance of a metaclass-The objc_msgsend () method will traverse the metaclass (if any) to determine which method is called. The class method is defined by the Meta class of a class object, just as the instance method is described by the representative class of an instance object.

What is the metadata? Does the metadata continue like this? No. A metadata class is an instance of a root metadata class, and a root metadata class is its own instance. The end cycle of the ISA pointer chain is as follows: from the instance to the class, from the class to the Meta class, then from the Meta class to the root class, the root class to its own. The ISA pointer behavior of meta-classes is rarely noticed, because no one sends messages to a meta-class in the real world.

A more important concept is the parent class of a metadata class. The meta-class parent class chain is parallel to the class parent class chain, so the inherited class methods are also parallel to the instance methods. The parent class of the root element class is the root class, so every class Object responds to the instance method of the root class. In the end, the object of a class is an instance of the root class (or a subclass of the root class), just like other objects.

Are you confused? This figure may help you. Remember, when a message is sent to any object, the method for sending the message starts from the ISA pointer of the object and continues to search for the parent class chain. Class description "instance method", while the Meta class and root class description "class method ".


According to the scientific computer language theory, a hierarchy of classes and meta-classes can be more free, deeper meta-class chains, and multiple classes instantiated from any single Meta-class. In objective-C, the metadata method is used as a normal class method, but it is more inclined to hide the metadata. For example, [nsobject class] and [nsobject self] are the same. Even in the form, it should return the Meta class referred to by nsobject-> ISA pointer. Objective-the C language uses a practical compromise method. Here it limits the class architecture to avoid getting too much.

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.