preface

To fully understand the Runtime mechanism, you need to understand the following:

  • What is Rnutime?
  • Why: Learning the Runtime mechanism?
  • How: Do you know How the Runtime works at the bottom?
  • Where: Where are the common Runtime application scenarios?

I will also explain the above points in order. Without further ado, the good ones have already liked it.

What is Runtime?

What is Runtime? As the name suggests, the concept is nothing more than “Because Objective-C is a dynamic language, it needs a runtime system… This is the Runtime system. For novice bloggers, the Runtime is actually a set of C functions. Hu Shi said: “more research problems, less talk about doctrine”, the concept of cloud mountain fog is always easy to dizzy, next we directly from the code to learn

Runtime. Objective-c deferred as many decisions as possible from compile time and link time to run time. Whenever possible, it does things dynamically. This means that the language requires not only a compiler, but also a runtime system to execute the compiled code. The runtime system acts as an operating system for objective-C; This is what makes language work.

Clang is a C, C++, and Objective-C compiler that includes preprocessing, parsing, optimization, code generation, assembly, and linking.

Compiler: clang-rewrite-objc

The middle code after coding.

Several dynamic runtime methods in the NSObject class

Why learn Runtime mechanism?

  • Before I started to share the Runtime, I hesitated for a long time whether to share the Runtime with the team. Because runtime is a double-edged sword, well understood and well used, it can write some efficient and practical code, and badly used, it can fool the team members. As they say, a long step is easy to pull the egg.
  • Learning Runtime is a necessary process for programmers to advance, not only OC, but also all languages to learn programming at a certain level, it is necessary to study the underlying compilation and operation mechanism of the language. What we say here is not to use the underlying method flexibly, but to understand and be familiar with the underlying operation mechanism. Therefore, we can only share some basic learning content, and then we can study by ourselves
  • Basic function points: 1. Exchange method, 2. Dynamic adding method, 3. Dictionary-to-model libraries are more or less compatible with Runtime.
  • Better understanding, classes, objects, messaging mechanisms, and dynamic interaction with code at run time: an essential Arsenal of dark magic!

How: How does Runtime work at the bottom?

  • Messaging
  • Dynamic Method Resolution
  • Message Forwarding
  • Type encoding & attribute declaration

    1. Messaging:

    1. Learn about the Runtime data structure! :

    1. Messaging Forward:

    1. Dynamic Method Resolution

Complete message forwarding mechanism

Talking is cheap Show Me the Code

Runtime API summary

Add link

  • 1. Type Encoding

To assist the runtime system, the compiler encodes the return and parameter types of each method as a string and associates that string with the method selector. It uses an encoding scheme that is also useful in other contexts and is therefore publicly available through the @encode() compiler directive. When given a type specification, @encode() returns the string encoding that type. The type can be a basic type, such as int, pointer, tag structure or union, or class name — in fact, any type can be used as an argument to the C sizeof() operator.

  • Dynamic Loading

Objective-c programs can load and link new classes and categories at run time. The new code is incorporated into the program and is the same as the classes and categories loaded at the beginning. Dynamic loading can be used to do many different things. For example, various modules in a system preference application are loaded dynamically.

Although there is a runtime function that dynamically loads objective-C modules in a Mach-o file (objc_loadModules, defined in objc/objc-load.h), But Cocoa’s NSBundle class provides a significantly more convenient interface for dynamic loading — an object orientation and integration with related services. For information about the NSBundle class and its use, see the NSBundle class specification in the Foundation Framework Reference. For information about Mach-O files, see OS X ABI Mach-O File Format Reference.

Where: Where are the common Runtime application scenarios?

  1. Implement the first scenario: keep track of how many times each ViewController is presented to the user. Replace the original ViewDidAppear Method with Method Swizzling. Create a class of UIViewController, override the custom ViewDidAppear method, and swap the ViewDidAppear method in its +load method
  2. The Associated Objects of the Runtime can be used to add new attributes to a class without changing it, especially for a system class
  3. Third, the dictionary model and automatic conversion, excellent JSON to model third-party libraries JSONModel, YYModel, etc., use runtime to obtain properties, assign values and other operations, more powerful than KVC model conversion, more efficient. Reading the source code of YYModel, it can be seen that YY god encapsulated the content of NSObject again and added many descriptions. YYClassInfo encapsulates Class again, while YYClassMethodInfo, YYClassMethodInfo, and YYClPropertyInfo encapsulate and describe ClassIvar, Method, and property respectively. The extraction

Basically that’s it. If you have any thoughts or mistakes, please leave a comment in the comments section and ask for a wave of likes. If you need information, please click below to get it.

IOS Information: Download address