Object-c class, object, Runtime, ISA

Source: Internet
Author: User

Keep a record of its bottom-up operations today.
1: Classes class:
typedef struct OBJC_CLASS * class;
As you can see from the definition of class, it is a pointer to a objc_class struct type, and what is Objc_class?

struct objc_class{struct objc_class* Isa;  struct objc_class* super_class;    The root is a null const char* name;    Long version;    Long info;    Long instance_size;    struct objc_ivar_list* ivars;  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 class also class, and does it have the same memory layout as the bottom object? This is the difference between a class object and an instance object (instance object).
Object-c the class object to the class structure pointed to by ISA in the instance object: The ISA in the class object points to the class structure called the static class member variable of the Metaclass,metaclass storage class and the static class member method (+) The ISA in the instance object is referred to as class structure (normal), the class structure stores the ordinary member variables of the classes and the ordinary member methods (-the method that begins).

2: Object ID:
typedef struct OBJC_OBJECT {Class isa;    } *id; It can be found that the ID can be used to represent any object, which is a pointer to a objc_object struct type whose first member is a pointer to a objc_class struct type.
Our root class NSObject is also the only one class member:
@interface NSObject <NSObject> {
Class Isa;
}
What the hell is this ISA? The official introduction is this:
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 message S.
As can be seen, an object's Isa points to the class of the object, and Isa for the object's class (class) points to Metaclass. So we can find static methods and variables.


3: Run Time:
The OBJECTIVE-C runtime is dynamic, allowing you to add methods or remove methods and use reflection at run time for a class. This is rare in other languages.

The ISA for the instance object of the class points to its class, the class's Isa points to the class's Metaclass, the class's super_class to its parent class, and the value is NULL if the class is the root class, and Metaclass Isa points to the root metaclass if the Metaclass is the root metaclass points to itself, Metaclass's super_class points to the parent metaclass, if the metaclass is the root metaclass point to the metaclass corresponding class;
OBJECT-C generates two objc_class for each class definition, one normal class, and the other is metaclass. We can create these two objc_class data structures at run time, and then use Objc_addclass to register class with the runtime system to dynamically create a new class.

Object-c class, object, Runtime, ISA

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.