Starting with this article, we are going to cover some of the major nodes in the Runtime, and once we get through these nodes, we will be able to take care of the problems we encounter in daily development. This series of articles also refer to official documents, MJ teacher and other sources.
What is Runtime
Objective-c Runtime Programming Guide
The Objective-C language defers as many decisions as it can from compile time and link time to runtime. Try to place the resolution at runtime rather than during compilation and linking.
The Runtime is a set of mechanisms provided by the runtime. So what exactly does this mechanism mean?
- From the perspective of code, this is a set of low-level C language API, usually written Objc code, are based on it to achieve.
- The following source code compilation and subsequent API interpretation is based on this description.
- From a compile-run perspective, the Runtime provides a number of features, mainly the messaging mechanism that makes Objc dynamic at runtime.
- We’ll look at these powerful dynamic capabilities in more detail.
There are two versions of Runtime: modern and Legacy. In 64-bit applications after iOS and macOS 10.5, Objective-C 2.0 uses the Modern version of the Runtime system. Older 32-bit maxOS programs still use the Objective-C 1.0 version of Legacy Runtime. The biggest difference between the two versions: When changing the layout of an instance variable of a class, in Legacy you need to recompile its subclasses, whereas modern does not. After obtaining the source code, a cursory look will reveal objc-Runtime-new. h and objc-runtime-old.h headers, as well as \__OBJC2__ or OBJC2_UNAVAILABLE, etc. These are the two versions of the Runtime in the source code.
Note: What follows is based on the Modern version.
How do I interact with Runtime
There are three ways to interact with the Runtime:
- Objective – C source code
- NSObject methods
- The Runtime method
1.1 Objective-C source code
Most of the time, the Runtime runs automatically in the background, so we use runtime by writing objective-C source code. When compiling classes and methods that contain Objective-C, the compiler generates data structures and functions that contain the Runtime. Data structures contain information defined from classes, categories, protocols, such as selectors, variables, and so on.
1.2 NSObject methods
Most of the classes in Cocoa inherit from NSObejct, inherit the methods defined by NSObject. The notable exception is the NSProxy class. Because of this, many of NSObject’s methods determine the behavior of other classes. NSObject is an abstract interface that defines methods, not implementation code. For example, description, you subclass it. Some methods of the NSObject class can query runtime information directly to obtain information about themselves. Class, is index of class, response to Selector, etc.
1.3 Runtime method
The Runtime system is a shared library containing a number of methods and data structures at /usr/include/objc. Many of these methods allow you to override the compilation behavior using C, while others are used through the NSObject class. With these methods, we can add interfaces, or tools, to the Runtime system.
Three, compile,
3.1 Where does the source come from
Apple’s official opensource site: openSource.apple.com
Apple Open Source site: contains Apple open source projects and tools, as well as documentation. Apple online source code download – source file compression package download – Apple objC source code
3.2 build
How do I compile objc’s source code?
- See the open source RetVal/ objC-Runtime on GitHub
- Do what I did: compile Runtime source code or objC4-750 Runtime source code
- Download objC4-Runtime which I compiled
After compiling the code, create a new target for later testing, as shown below:
After that, add the compiled objC project:
reference
link
- Objective-C Runtime Programming Guide
- Objective-C Runtime reference
- Mach-O Programming Topics
- Apple Open Source site: contains Apple open source projects and tools, as well as documentation.
- Apple Online source code
- Download – source file compression package
- Download – Apple objC source code
code
- objc4-runtime