Objects classes and metaclasses in objective-C

Source: Internet
Author: User

 

 

Objective-C is a class-based object system. Each object is an instance of some class; the object'sisaPointer Points to its class. that class describes the object's data: allocation size and Ivar types and layout. the class also describes the object's behavior: The selectors it responds to and instance methods it implements.

The class's method list is the set of instance methods, the selectors that the object responds to. When you send a message to an instance,objc_msgSend()Looks through the method list of that object's class (and superclasses, if any) to decide what method to call.

Each objective-C class is also an object. It hasisaPointer and other data, and can respond to selectors. When you call a "class method" like[NSObject alloc], You are actually sending a message to that class object.


Since a class is an object, it must be an instance of some other class: A metaclass. the metaclass is the description of the class object, just like the class is the description of ordinary instances. in particle, the metaclass's method list is the class methods: the selectors that the class Object responds. when you send a message to a class-an instance of a metaclass-objc_msgSend()Looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the Instance objects.

What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class's metaclass; the root metaclass is itself an instance of the root metaclass.isaChain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclassisaPointers rarely matters, since in the real world Nobody sends messages to metaclass objects.

More important is the superclass of a metaclass. the metaclass's superclass chain parallels the class's superclass chain, so class methods are inherited in parallel with instance methods. and the root metaclass's superclass is the root class, so each class Object responds to the root class's instance methods. in the end, a class object is an instance of (a subclass of) the root class, just like any other object.

Confused? The digoal may help. Remember, when a message is sent to any object, the method lookup starts with that object'sisaPointer, then continues up the superclass chain. "instance methods" are defined by the class, and "class methods" are defined by the metaclass plus the root (non-meta) class.

In proper computer science language theory, a class and metaclass hierarchy can be more free-form, with deeper metaclass chains and multiple classes instantiated from any single metaclass. objective-C uses metaclasses for practical goals like class methods, but otherwise tends to hide metaclasses. for example,[NSObject class]IS IDENTICAL[NSObject self], Even though in formal terms it ought to return the metaclass thatNSObject->isaPoints to. The objective-C language is a set of practical compromises; here it limits the class schema before it gets too, well,Meta.

From-> http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

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.