Objective-c ISA pointer and runtime mechanism

Source: Internet
Author: User

One. Isa pointer

To understand what Isa pointers are, we have to make a point:

In objective-c, the definition of any class is an object. There is no intrinsic difference between a class and an instance (object) of a class. Any object has an Isa pointer.

So what is a class? Using the shortcut key shift+cmd+o in Xcode to open the file objc.h see the definition of the class:


Can be seen:

The Class is a pointer to a objc_class struct type, and the ID is a pointer to a objc_object struct type.

Let's look at the definition of Objc_class:


Explain the meaning of each parameter a little bit:

ISA: Is a class-type pointer. Each instance object has a pointer to Isa, which points to the class of the object, and the class also has a pointer to Isa, pointing to Meteclass (the Meta Class). The Meta class holds a list of class methods. When a class method is called, the implementation of the class method is looked up first from itself, and if not, the meta-class looks for the method to his parent class. Also note that the meta-Class (Meteclass) is also a class, and it is also an object. the Meta class also has an Isa pointer, and its ISA pointer eventually points to a root class (root meteclass). The ISA pointer to the root class points to itself, which forms a closed inner loop.

Super_class: The parent class, which is null if the class is already the topmost root class.

Version: The release information for the class, which defaults to 0

Info: Some bit IDs for use during the run time.

Instance_size: Instance variable size for this class

Ivars: An array of member variables

Let's take a look at the inheritance relationships of each class instance variable:


Each object is essentially an instance of a class. Where the class defines the list of member variables and member methods. The object points to the class through the object's ISA pointer.

Each class is essentially an object, and the class is actually an instance of the Meta Class (Meteclass). A meta-class defines a list of class methods. The class points to the Meta class through the ISA pointer of the class.

All meta-classes eventually inherit a root class, and the root-class Isa pointer points to itself, forming a closed inner loop.

two. Runtime mechanism

Runtime: Refers to the state in which a program is running (or being executed). That is, when you open a program to make it run on a computer, that program is in the running moment. In some programming languages, some reusable programs or instances can be packaged or rebuilt into a "runtime". These instances can be connected or called by any program while they are running.

OBJECTIVE-C Runtime: is a relatively low level of pure C language API, belongs to 1 C language Library, contains a lot of the underlying C language API. In the OC code that we normally write, when the program runs, it turns into the C language code of runtime.

Runtime Applications:

1. Dynamically create a class (such as the underlying implementation of KVO)

2. Dynamically add properties \ Methods to a class, modify property values \ Methods

3. Traverse all member variables (properties) of a class \ All methods

In essence, the above is accomplished by obtaining an Isa pointer to an object or class by means of a related method.

Related functions

1. Increase

Add function: Class_addmethod

Add Instance variable: Class_addivar

Add attribute: @dynamic tag, or Class_addmethod, because the property is actually made up of getter and setter functions

Add Protocol:class_addprotocol (Honestly I don't know what's the use of adding a protocol dynamically,-_-!!)

2. Get

Get a list of functions and information about each function (function pointers, functions names, and so on): Class_getclassmethod method_getname ...

Get the property list and information for each property: Class_copypropertylist property_getname

Get information about the class itself, such as class name: Class_getname class_getinstancesize

Get variable list and variable information: class_copyivarlist

Get the value of a variable

3. Replace

Replace the instance with another class: Object_setclass

Definition of Substitution class method: Class_replacemethod

4. Other common methods:

Exchange two implementations of the methods: Method_exchangeimplementations.

Set the implementation of a method: Method_setimplementation.



Wen/Counin (Jane book author)
Original link: HTTP://WWW.JIANSHU.COM/P/41735C66DCCB
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Objective-c ISA pointer and runtime mechanism

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.