Objc Class And Metaclass (Objective-C Class And original Class)

Source: Internet
Author: User

Objective-C is a class-based object-oriented language. Each object is an instance object of a class. The isa pointer of the object points to the class corresponding to the object. Class describes the object data: memory allocation size, variable (ivar) type and layout. Class also describes the behavior of the object: class response selector (selectors are translated literally because selector is not the starting address of the specific implementation of the method, but the starting address of the method corresponding to a key) and the specific method of class implementation.

The method list of a class is a collection of method instances and Selector objects that can respond. When you send a method to an instance object, objc_msgSend () queries the list of methods of the class corresponding to the object (if there are superclasses, it also queries the list of superclasses) to determine which method to call.

Every Objective-C class is also an object. It also has an isa pointer and other data, and can be a corresponding selector. When you call a "class method" such as [NSObject alloc], a message is indeed sent to the Class Object (not the class instance object.

Since a class is also an object, it must also be an instance of another class: the original class. The original class is the description of the class object, just like the description of the class as the original Instance Object. In particular, the method list of the original class is the class method: the class object can have a corresponding selector. When you send a message to a class-the instance of the original class-objc_msgSend (), you can query the method list of the original class (if there are superclasses, you can also query the list of superclasses) to determine the specific method to call. Class methods are described by the original class that represents the class object. Just as the object instance method is described by classes that represent instance objects.

What about the original class? Is the original class the deepest implementation? No. The original class is an instance of the original class of the root class; the original root class is an instance of its own. This isa transfer ends here: the object instance-class-original class-root original class itself. The behavior of the isa pointer of the original class does not matter much. Generally, few people send messages to the original Class Object.

More importantly, the superclass of the original class. The original super-class chain is in parallel with the super-class chain of the class, so the class method is also in parallel with the instance method. The super class of the original Root class is the root class, so each class object can correspond to the methods of the root class instance. Finally, the class object is an instance of the root class (or subclass), just like any other object.

Confused? The figure below will help you understand. Remember, when a message is sent to any object, the method query starts from the isa pointer of the object, and then continues the query from the super-class chain. The "instance method" is defined by the class, and the "class method" is defined by the original class plus the root class (not the original.

In appropriate computer science language theories, the hierarchy of classes and original classes can be more free, has a deeper original class chain and multiple classes from any simple original class instance. Objective-C uses the original class as a class method for the actual purpose, but hides the original class. For example, [NSObject class] is equal to [NSObject self], although it should return the original class that NSObject-> isa points. The Objective-C language is a set of discounts. The class architecture is limited before too, well, and meta are obtained.


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

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.