Runtime of objective-C 2-Understanding classes and objects in oC

Source: Internet
Author: User

 

1: Class class:
Typedef struct objc_class * class;
From the definition of Class, we can see that it is a pointer to the objc_class structure type. What is objc_class?

Struct objc_class {struct objc_class * ISA; struct objc_class * super_class; // The root value is null const char * Name; long version; long Info; long instance_size; struct objc_ivar_list * ivars; // instance variable struct objc_method_list ** methodlists; // method list struct objc_cache * cache; // cache recently used methods to improve efficiency; struct objc_protocol_list * protocols ;};

 

Why is the first member of the class also a class? Isn't its memory layout the same as that of the bottom object? In fact, this is the difference between Class Object and instance object.
Object-C has different names for the class structure pointed to by the class object and the ISA in the instance object: The ISA pointing class structure in the class object is called metaclass, metaclass static class member variables of the storage class and static class member methods (Methods Starting with +); ISA in the instance object points to the class structure called class (common ), the class structure stores the common member variables of the class and the common member methods (the method starting ). note: Methods, attributes, and Protocols are stored in writable segments of the class definition. The information can be changed at runtime. This is also the implementation principle of classification. Ivar is stored in read-only segments, so it cannot be modified. This is the reason why the class cannot add instance variables. In the following example, the nsobject response selector list is printed.
void PrintObjectMethods(){    unsigned int count = 0;    Method *methods = class_copyMethodList([NSObject class], &count);    for (unsigned int i =0; i < count; ++i) {        SEL sel = method_getName(methods[i]);        const char *name = sel_getName(sel);        printf("%s\n",name);    }    free(methods);}

 



2: Object ID:
     typedef struct objc_object {         Class isa;     } *id;

 

It can be found that the ID can be used to represent any object. It is a pointer of the objc_object structure type, and its first member is a pointer of the objc_class structure type.
Our root class nsobject also has only one class member:
@ Interface nsobject <nsobject> {
Class ISA;
}
What is Isa? The official introduction is as follows:
Every object is connected to the run-time system through its IsaInstance variable, inherited from the nsobject class. IsaIdentifies the object's class; it points to a structure that's compiled from the class definition. Through Isa, An object can find whatever information it needs at run timesuch as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages.
It can be seen that the ISA of an object points to the class of this object, and the ISA of this object class points to metaclass. In this way, we can find static methods and variables.

3: runtime:
Objective-C is dynamic during runtime. It allows you to add methods or remove methods and use reflection for classes during runtime. This is rare in other languages.

ISA of the class instance object points to its class; ISA of the class points to metaclass of the class; super_class of the class points to its parent class. If the class is the root class, the value is null; the ISA of metaclass points to the root metaclass. If the metaclass is the root metaclass, it points to itself. The super_class of metaclass points to the parent metaclass;
Object-C generates two objc_class for each class definition, one common class and the other metaclass. We can create the two objc_class data structures at runtime, and then use objc_addclass to register the class into the runtime system, so as to dynamically create a new class.
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.