recently participated in three interviews have been asked to the runtime, do not know the interviewer themselves understand, anyway, like to ask, seems to want to put me bluffing, to show their understanding of OC bottom knowledge is very good. (I now start to look at the interview questions, found that they all like to ask the question of interview questions, I figured out, are test-oriented education to provoke the curse, probably they have no actual experience in the app, so prepared a lot of interview questions, and I really can write apps, first work to find ways to people, interview not much, no interview experience, It seems to start hitting the wall many times to master the Chinese interview. Because I do not understand the runtime, I can only say a few bits and pieces. I really try to look at the runtime's knowledge, because know understand it to understand the OC code internal changes have some help, but really feel that the development is not meaningful and useful, but also pure C code, now open-source projects so many, basic can refer to other people's code to achieve their own purposes, I wonder if these interviewers are often using runtime to dynamically change the code in the program, and they are developing apps that are so awesome. But for the interview, today is a bit of a little learning, but still do not study to let their satisfaction, or do not understand how to dynamically change the method or variable inside the program. Barely write today's a little harvest summary, still very fragmented is, crossing please help yourself. I found this article good, read a few articles about the Runtime, this article I feel a little easier to understand a little, address: "objective-c characteristics: Runtime", if you want to learn more please refer to this article.
What is runtime (runtime mechanism)
Objective-c is a dynamic language which is based on the C language, which means that it needs not only a compiler, but also a runtime system to dynamically create classes and objects for message sending and forwarding.
1.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.
2. Usually write the OC code, in the process of running the program, in fact, eventually turned into the runtime C language code, runtime is OC's behind-the-scenes workers
The role of the runtime
Runtime is the bottom of the OC and can perform some very low-level operations (with OC is impractical and not good to achieve).
1. Dynamically create a class during the program's run (for example, the underlying implementation of KVO)
2. Dynamically add properties \ Methods to a class, modify property values \ Methods during program run
3. Traverse all member variables (properties) of a class \ All methods
Runtime related applications
* Nscoding (archive and file, use Runtime to traverse all properties of Model objects)
* Dictionary-to-model (use runtime to traverse all properties of the model object, remove the corresponding value from the dictionary based on the property name, set to the properties of the model)
* KVO (using runtime to generate a class dynamically)
* For packaging frame (how to change how to change)
Runtime data structure
In Objective-c, using the [receiver message] syntax does not immediately execute the code of the receiver object's message method, but instead sends a message to receiver that may be handled by receiver. It may also be forwarded to other objects for processing, or it may be possible to pretend that the message was not received and not processed. In fact [receiver message] is translated by the compiler to:
ID ID Self, SEL op, ...);
The following is a brief introduction to the important data structures in runtime:
|
SEL |
represents the method selector. In fact, it is the c string, you can pass objc compiler command @selector () or runtime system sel_registername function to get a sel type of method selector. |
|
id |
id is a generic type pointer that can represent any object. id > Actually, it's a pointer to objc_object struct pointer, which contains a class Isa member, according to isa |
|
Class |
Class represents the class to which the object belongs. You can see that the Class is actually a objc_class struct pointer. In object-oriented design, everything is an object, andClass is itself an object in design. |
|
Method |
method represents a method in a class. In fact method is a pointer to objc_method struct pointer, which stores the method name (method_name) , method type (method_types) and method implementation (Method_imp) and other information. |
|
ivar |
ivar represents an instance variable in a class. ivar > Actually, it's a pointer to objc_ivar struct pointer, which contains the variable name (Ivar_name) , variable type (Ivar_type) and other information. |
|
imp |
imp is essentially a function pointer, pointing to the implementation of the method. When you send a message to an object, you can specify the implementation of the method by the function pointer, which eventually executes the code so that you can bypass the message delivery phase and execute another method implementation. |
|
Cache |
Cache primarily used for caching. the Cache is actually a list of methods that are stored, primarily to optimize the performance of method calls. When the method is called, it takes precedence over the Cache Lookup, if it is not found, and then to the methodlists lookup. |
Runtime message Sending
In front of the objc_msgsend as the entrance, and gradually in-depth analysis of the data structure of the runtime, understand the role of each data structure and the relationship between them, we formally transferred to the message to send this topic.
Objc_msgsend function
As mentioned earlier, when an object uses the syntax [receiver message] to invoke a method, [receiver message] is converted by the compiler to:
ID ID Self, SEL op, ...);
Now let's take a look at objc_msgsend it specifically how to send a message:
1. First obtain its corresponding class according to the receiver object's ISA pointer
2. First find the message method in the class's cache, if not found, then to methodlists find
3. If not found in class, then go to Super_class to find
4. Once the message is found, the method implements the IMP as it is implemented.
Method parsing and message forwarding
[Receiver message] when invoking a method, what if the message method does not find a method in the class inheritance system of the receiver object? In general, the program at run time will be crash off, throw unrecognized selector sent to ... An exception message like this. But before you throw an exception, there are three chances to save your program in the following order.
1.Method Resolution
2.Fast Forwarding
3.Normal Forwarding
Three ways to choose
Runtime provides three ways to replace the original method implementation, so how to choose them?
1.Method Resolution: Because Method Resolution cannot be handed over to other objects as message forwarding, it is only applicable in the original class.
2.Fast Forwarding: It can forward message processing to other objects, using a wider range, not just the original object.
3.Normal Forwarding: It can be forwarded as fast as Forwarding, but it can get more information from the Nsinvocation object, such as: Target, selector, Information such as arguments and return values.
Method swizzling
Method Swizzling is the implementation of Another method that replaces the implementation of one approach at run time. If you can take advantage of this technique, you can write simple, effective, and maintainable code.
Summarize
Although the Objective-c runtime feature is not often used in regular projects, when you read some of the iOS open source projects, you will find that it is always used in many cases. So an in-depth understanding of OBJECTIVE-C's runtime data structure and message forwarding mechanism will help you to read and learn more about open source projects more easily.
Runtime run-time mechanism for iOS development