Exploration of IOS underlying principles – basic use of runtime


Runtime. OC is a runtime mechanism, that is, some mechanisms at runtime, the most important of which is the message mechanism.
For C language, the function call will decide which function to call when compiling.
The functions in OC language belong to dynamic calling process. When compiling, you can’t decide which function to call. Only when running, you can find the corresponding function according to the function name to call.

In the compilation stage, C language calls unimplemented functions and will report errors.
In the compilation stage, OC can call any function, even if the function is not implemented, as long as it is declared, it will not report an error.

There are five main functions of runtime

Send message (access private function)
Exchange method
Dynamic addition method
Add attribute to classification
Dictionary to model

1. Send message (access private function)

The essence of method call is to let the object send messages, and the OC underlying implementation sends messages through the runtime implementation.
The final message generation mechanism is what the compiler needs to do.
You can use clangclang – rewrite objc main M recompile the current code and view the final generated code.

NSObject *obj = [NSObject alloc];
obj = [obj init];

Eventually converted to

NSObject *obj = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("alloc"));
obj = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)obj, sel_registerName("init"));

After simplification, it can become

NSObject *obj = objc_msgSend(objc_getClass("NSObject"), sel_registerName("alloc"));
obj = objc_msgSend(obj, sel_registerName("init"));

objc_ Msgsend send message
objc_ GetClass gets the body of the sent message
sel_ Registername the number of the message sent

Of course, when we use it, we don’t need the above complex format. Generally, we can use it as follows:

NSObject *obj = objc_msgSend([NSObject class], @selector(alloc));
obj = objc_msgSend(obj, @selector(init));

Basic steps for using Runtime:

  1. To import a header file into a file:#import
  2. Set objc in building settings_ Msgsend is changed to no (apple can use runtime before xcode6, but Apple does not recommend runtime after xcode6)


Under what circumstances do we use the runtime message sending mechanism?
Usually, weWhen you need to access a private function, you can use runtime



Method call process:

[person eat]; What is a calling procedure?
First of all, you should understand that the object method list is in the class object. The class method list is in the metaclass.

In the person object, find the yzperson class object of person through the ISA pointer
Registration method No.: sel_ registerName(“eat”)
Find the corresponding method in the method list according to the method number
Find the method address and call the function in the method area


2. Exchange method

Exchange class method


Exchange object method


Exchange graph


The exchange is actually from the method list to the method implementation.

3. Dynamic addition method


4. Add attribute to classification

@The role of property in classification: it only adds the declaration of set and get methods, but does not implement them. Underline member properties are also not generated.


5. Dictionary to model

Recommended Today

2021-11-09 volcano map based on RNA SEQ table (second time)

setwd(“C:\\Users\\Administrator.DESKTOP-4UQ3Q0K\\Desktop”) library(“readxl”) data <- read_excel(“RNA-seq.xlsx”) library(dplyr) library(ggplot2) library(ggrepel) data #Convert to tibble for subsequent use and remove unnecessary columns; Data < – Data [C (- 10, – 11, – 14, – 15, – 16, – 19, – 20, – 21, – 22)] # don’t try #data <- as_tibble(data[c(-10,-11,-14,-15,-16,-19,-20,-21,-22)]) data$padj<-as.numeric(as.matrix(data$padj)) #Take logarithm of Q value; data$log10FDR […]