Meta classes (META Class)

Source: Internet
Author: User

Meta classes (Meta Class), this name presumably many people have heard, there are many on the internet on the introduction of the meta-class, today I will follow their own understanding of these two days to briefly explore this thing, the wrong place also hope to point out.

First, download OBJC source code, source address: https://opensource.apple.com/tarballs/objc4/
After opening the link will find a lot of versions, I directly download the latest version (709 version)

Meet NSObject
1. Open the NSObject.h of the OBJC project and find the definition of NSObject class @interface NSObject <NSObject> {class ISA objc_isa_availability;Discovery NSObject contains a class object}2. Continue to view class typetypedefstruct Objc_class *class;3. Continue to view Objc_class typesstruct Objc_class:objc_object {//objc_class inherits from Objc_object Class superclass; Const Char *name; uint32_t version; uint32_t Info ...} 4. View objc_object type struct Objc_object {private: isa_t Isa; //This thing seems very good, continue to look ...} 5. View isa_t type Union isa_t { isa_t () {} isa_t (uintptr_t value): Bits (value) {} Class CLS ; //Eh, this thing appears again uintptr_t bits;              

Simply use the picture to indicate:


Figure-1


So a simple summary is that nsobject contains a class,class that contains a superclass and CLS

Meta class get
1. To open OBJC class Get interface class objc_getmetaclass (const Span class= "Hljs-keyword" >char *name) {//The following code simplifies //1. The class object is found through the class name (note that it is not a class instance object) class cls = Objc_getclass (aclassname); //2. Returns the innermost CLS return cls->isa.cls;} 2.meta class is actually getting the CLS that the class object contains, and we cannot invoke the CLS in the actual development, Can be achieved by Object_getclass. View Object_getclass implementation class object_getclass (id obj) {// The following code has been simplified return obj->isa.cls; //also returns CLS}             
To analyze the source code by testing it
Introducing the Rumtime header file#import<objc/objc-runtime.h>To define a simple class@interfaceFather:NSObject@property (nonatomic, strong) NSString * Name  @end //various instances print father* f = [[Father alloc] init]; nslog (@" F address:%p ", f); nslog (@ "[F class] address:%p", [F class]); nslog (@ "[Father class] address:%p", [Father class]); nslog (@ "Objc_getmetaclass address:%p", Objc_getMetaClass ( Span class= "hljs-string" > "Father")); nslog (@ "Objc_getclass address:%p", Object_getClass ([Father class]);             

Printing results:

Printed results: 2017-04-26 11: 46: 37.104Runtimedemo[190:22,480,332]FAddress: 0x600000011e702017-04-26 11: 46: 37.104Runtimedemo[190:22,480,332][F class]Address: 0x1063d5ff02017-04-26 11: 46: 37.105Runtimedemo[190:22,480,332][Father class] address: 0x1063d5ff02017-04-26:37.105 runtimedemo[190:22,480,332 ] objc_getmetaclass address: 0x1063d5fc82017-04-26:37.105 runtimedemo[ 190:22480332] objc_getclass address: 0x1063d5fc8        

Analysis through the results:

    • The class instance object itself is not a meta-class
    • Class instance objects are obtained through the class method as object classes, [f class] = = [Father class]
    • A object_getclass called by a class object is given a meta class
Meta class inheritance
Meta class inheritance diagram

This is a very good diagram, we use the simplest possible code to test:

Son *s = [[Son alloc] init]; Instance of Subclass Class CLS = [S class]; Subclass class Class Meta = Object_getclass (CLS); Subclass Meta Class superclass = [CLS Superclass]; Class Supermeta = [Meta superclass]; Class supermeta2 = Object_getclass (superclass); Class Rootclass = [Superclass superclass]; Class Rootmeta = [Supermeta superclass]; Class rootmeta2 = Object_getclass (Rootclass); Class Nilclass = [Rootclass superclass]; Class Superrootmeta = [Rootmeta superclass]; NSLog (@"s address:%p", s); NSLog (@"CLS Address:%p", CLS); NSLog (@ "meta address:%p", Meta)  "superclass address:%p", superclass)  "Supermeta address:%p", Supermeta)  "Supermeta2 address:%p", supermeta2)  "Rootclass address:%p", Rootclass)  "Rootmeta address:%p", Rootmeta)  "Rootmeta2 address:%p", rootmeta2)  "Nilclass address:%p", Nilclass)  "Superrootmeta address:%p", Superrootmeta)   

Print results

2017-04-26 12: 32: 10.412Runtimedemo[1194:22,584,480]SAddress: 0x6080000157002017-04-26 12: 32: 10.412Runtimedemo[1194:22,584,480]ClsAddress: 0x10540f0602017-04-26 12: 32: 10.412Runtimedemo[1194:22,584,480]MetaAddress: 0x10540f0382017-04-26 12: 32: 10.412Runtimedemo[1194:22,584,480]SuperclassAddress: 0x10540f0b02017-04-26 12: 32: 10.413Runtimedemo[1194:22,584,480]SupermetaAddress: 0x10540f0882017-04-26 12: 32: 10.413Runtimedemo[1194:22,584,480]Supermeta2Address: 0x10540f0882017-04-26 12: 32: 10.413Runtimedemo[1194:22,584,480]RootclassAddress: 0x105da8e882017-04-26 12: 32: 10.413Runtimedemo[1194:22,584,480]RootmetaAddress: 0x105da8e382017-04-26 12: 32: 10.413Runtimedemo[1194:22,584,480]rootmeta2 address: 0x105da8e382017-04-26:10.413 runtimedemo[1194:22,584,480] nilclass address: 0x02017-04-26:10.413 runtimedemo[1194:22584480]  Superrootmeta address: 0x105da8e88           

The results show that the results are consistent with the illustrations.

FAQ:
Is there a difference between 1.class method and Object_getclass?
A careful friend may have found it, sometimes using the class method, sometimes using the Object_getclass method. Let's take a look at the source code

+ (Class)class {    return self;}- (Class)class { return object_getClass(self);}
    • class method, which returns self, so when looking for a meta class, you need to call the Object_getclass method on the class object
    • Example method class, the internal implementation is called the Object_getclass method,
      When an instance object calls class or uses Object_getclass () on an instance object, it is actually the CLS of the instance object that is returned, but the CLS inside the instance object holds the class object instead of the Meta class

PS: Attach some articles about Meta class
http://www.jianshu.com/p/45fe90253519
http://blog.csdn.net/beclosedtomyheart/article/details/50164353
http://blog.csdn.net/windyitian/article/details/19810875

Tuple (Meta Class)

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.