Full explanation of runtime Mechanism

Source: Internet
Author: User

We have already talked about a runtime principle. Now this article mainly introduces what Runtime is and how to use it! Hope to help readers!

First, the first question,
1. What is the mechanism of runtime implementation? How to use it?
I will not go around with you about this issue. I will tell you directly,
Runtime is a relatively low-level pure C language API. It belongs to a C language library and contains many underlying C language APIs.
In the OC code we usually write, when the program is running, it is actually converted into the C language code of runtime. Runtime is the behind-the-scenes worker of OC.
For example, in the following method to create an object,
Example:
OC:
[[Mjperson alloc] init]
Runtime:
Objc_msgsend ("mjperson", "alloc"), "init ")

Second question
What is runtime used ?? Where are they used? How to use it?
Runtime is the bottom layer of OC and can be used for some very bottom-layer operations (it is impossible to use OC for implementation)

  • Dynamically create a class (such as the underlying implementation of KVO) when the program is running)

  • When the program is running, it dynamically adds properties \ methods to a class and modifies the property value \ methods.

  • Traverse all member variables (attributes) \ all methods of a class
    For example, when we need to archive and archive the attributes of a class, there are many attributes. At this time, we will write a lot of corresponding code, but if we use runtime, we can set it dynamically!
    For example, the pyperson. h file is as follows:

    Import

@ Interface pyperson: nsobject
@ Property (nonatomic, assign) int age;
@ Property (nonatomic, assign) int height;
@ Property (nonatomic, copy) nsstring * Name;
@ Property (nonatomic, assign) int age2;
@ Property (nonatomic, assign) int height2;
@ Property (nonatomic, assign) int age3;
@ Property (nonatomic, assign) int height3;
@ Property (nonatomic, assign) int age4;
@ Property (nonatomic, assign) int height4;

@ End

The content of the pyperson. m implementation file is as follows:

#import "PYPerson.h"
Import

@ Implementation pyperson

  • (Void) encodewithcoder :( nscoder) Encoder
    {
    Unsigned int COUNT = 0;
    Ivar
    Ivars = class_copyivarlist ([pyperson class], & COUNT );

    For (INT I = 0; I <count; I ++ ){

    // Retrieve the member variable Ivar = ivars [I] corresponding to the I position; // view the member variable const char * name = ivar_getname (Ivar ); // archive nsstring * Key = [nsstring stringwithuf8string: Name]; id value = [self valueforkey: Key]; [encoder encodeobject: Value forkey: Key];

    }

    Free (ivars );
    }

  • (ID) initwithcoder :( nscoder *) Decoder
    {
    If (Self = [Super init]) {

    Unsigned int COUNT = 0; Ivar * ivars = class_copyivarlist ([pyperson class], & COUNT); For (INT I = 0; I <count; I ++) {// retrieve the member variable Ivar = ivars [I] corresponding to the I position; // view the member variable const char * name = ivar_getname (Ivar ); // archive nsstring * Key = [nsstring stringwithuf8string: Name]; id value = [decoder decodeobjectforkey: Key]; // set it to the member variable [self setvalue: Value forkey: key];} Free (ivars );

    }
    Return self;
    }

@ End

In this way, we can see that the archive and archive case is actually written in runtime.

Learning: The runtime mechanism should first understand the following issues:
1. Related header files and functions
1> header file


  • Using the header file, we can view the methods in runtime!

2> related applications

  • Nscoding (archive and archive, use runtime to traverse all attributes of the model object)
  • Dictionary-> model (use runtime to traverse all the attributes of the model object, retrieve the corresponding values from the dictionary based on the attribute name, and set them to the attributes of the Model)
  • KVO (use runtime to dynamically generate a class)
  • Used to encapsulate the framework (how to change it)
    This is what we use as long as we use the runtime mechanism.

3> related functions

  • Objc_msgsend: send messages to objects
  • Class_copymethodlist: traverses all methods of a class.
  • Class_copyivarlist: traverses all member variables of a class.
  • Class _.....
    This is a required function for learning runtime!

4. Common sense
1> Ivar: member variable
2> method: Member Method
From the above example, we can see the defined member variables. If you want to dynamically create a method, you can use method,

Maybe, do you have a better understanding of runtime? Here, we hope you can communicate with each other! Please correct me for any errors
Contact information: [email protected]

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.