The opening

I spent a lot of time to sort out the “memory management” and “Runtime” related issues, also recorded on the platform, below the address, but then feel this way some of the work, the problem is that these problems, after I finish see on video, combined with the platform, and equivalent to copy again and again to do so is meaningless, I have basically watched each video for several times, and each time I have a new understanding, but WHEN I ask specific questions, I still cannot tell them completely and remember them. The text version has been sorted out by someone on the platform, and I am putting it together, so far, it does not have much effect.

The main purpose of these things is for the InterView. Yes, this is the most direct purpose.

So what I really need is not a blogo-style explanation, but to be able to answer, to develop my own language for specific questions, that is, I find questions and then I answer them.

Methods adopted:

  1. Combined with the questions I met in the actual interview and the online questions, I answered them in my own language
  2. Combine the existing video and text version of the explanation, repeatedly pondering, and find the answer
  3. The number of columns of relevant excellent blogs and articles

IOS Advanced – Runtime

Refer to the link

An in-depth look at the iOS interview questions

1. IOS Interview questions (12) Runtime — Basic Data Structure 2. IOS Interview questions (13) Runtime — Object, Class object, metaclass object and message passing mechanism 3. IOS interview question (16) Runtime — method-swizzling 6. IOS Interview question (17) Runtime — Dynamic add methods & dynamic Method parsing

Blog, walking boy lang

1. IOS development: “Runtime” (a) Basic knowledge 2. IOS development: “Runtime” (B) Method Swizzling 3. IOS development: “Runtime” (C) Category basic principles 4. (4) Obtain the detailed properties and methods of the class

The body of the

1. Describe the process of calling object methods

In Objective-C, object method calls are [Receiver Selector]; Is essentially the process of having an object send a message at run time.

It is divided into compilation phase and runtime phase

Compile phase: [Receiver Selector]; Methods are converted by the compiler to:

  • Objc_msgSend (receiver, the selector)(Without parameters)
  • Objc_msgSend (recevier, selector, org1, org2...)(with parameters)

Runtime phase: The message receiver recever looks for the corresponding selector

  1. Find the class object of the current instance based on its ISA pointer
  2. We look in the cache, we look in the method cache of the class for the corresponding method implementation, and the lookup in the cache is a hash lookup, and if there is one, we call the function through the function pointer, and we’re done passing the message.
  3. If it is not found in the method cache, it continues to look in the class’s list of methods, sorted for binary lookup, and unsorted for general lookup. If it is found, it populates the method cache and returns the method implementation, ending the message delivery.
  4. If it is not found in the class’s method list, the current class object’ssuperClassThe pointer looks up the parent class hierarchically, again in the cache, and then the list of parent methods
  5. Once it’s found, it’s executedreceverCorresponding method implementation
  6. Step by step, search until nil, if none, enter the message forwarding process

When a method cannot be found, the Runtime provides dynamic message parsing, message receiver redirection, and message redirection during the message forwarding phase

  1. Dynamic message parsing,

Add additional function implementations using the class_addMethod method by overriding the +resolveInstanceMethod: or +resolveClassMethod: method;

  1. Message receiver redirects,

If the previous step to add other functions, can be used in the current object – forwardingTargetForSelector: approach the recipient of the message forwarded to other objects;

  1. Message redirection,

Message redirect: if the return value is nil, use – methodSignatureForSelector: method for function arguments and return values of type.

  • if-methodSignatureForSelector:Returns aNSMethodSignatureObject (function signature), which the Runtime system createsNSInvocationObject and pass-forwardInvocation:The message notifies the current object, giving the message sender one last chance to find the IMP.
  • if-methodSignatureForSelector:Returns nil. The Runtime system emits-doesNotRecognizeSelector:Message, and the program crashes.

So we can forward the message in the -forwardInvocation: method.

Note 1:

Here +resolveInstanceMethod: or +resolveClassMethod: either returns YES or NO, as long as NO other function implementation is added, the runtime proceeds to the next step.Copy the code

Note 2:

Since - forwardingTargetForSelector: and - forwardInvocation: all messages can be forwarded to other objects, then where is the difference between the two? The difference is that - forwardingTargetForSelector: only forward the message to an object. -forwardInvocation: Messages can be forwarded to multiple objects.Copy the code

2. How do isa Pointers find corresponding classes

It’s the Commons on C++, in OC it’s isa_t whether it’s 64-bit or 32-bit, the Commons is 64 or 32 zeros or ones

There are two types of ISA

  1. Pointer ISA: the 64-bit 0 or 1 as a whole represents the address of the Class to which it points. That is, the address of the Class object can be obtained from the contents of isa
  2. Non-pointer ISA: The worth part of isa represents the address of the Class. The reason for this is that only 30 or 40 bits are needed to ensure that we find all the addresses of the Class. The extra bits can be used to store other relevant information to save memory

3. After finding the corresponding implementation in the method list, whether to save it in the cache of the current class or in the parent class

When found in the list of methods of the current class, it is saved to the cache of that class

4. Under what conditions does the message forwarding phase enter

The important thing about this problem is that whether an instance method or a class method is called needs to be described separately

  1. If an instance method is called, the system will find its class object according to the isa pointer of the current instance, and traverse the method list in the class object. If no method is found, the system will follow the SubClass pointer to search the method list of the parent object until it reaches the method list of the root class object. If no method is found, the system will search the method list of the root class object. The message forwarding process is displayed
  2. If a class method is called, the system will look for the metaclass object through the ISA pointer of the class object, and then traverse the list of methods to the root metaclass object, and then to the root metaclass object. If there is no method implementation, the message forwarding process will be entered
  3. The difference between a traversal instance method and a class method is that in the root metaclass, the superClass that points to the root metaclass refers to the root object

5. If we call a class method with no corresponding implementation, but an instance method with the same name, will it crash? Will there be an actual call?

  1. The reason for the actual call is that the superClass pointer to the root metaclass object points to the root metaclass object, so if the implementation of the class method cannot be found in the metaclass, it will follow the superClass pointer to the list of instance methods
  2. If there is an instance method of the same name, it will actually be called

6. Is there a unified way to deal with this kind of unsolved crash

7. What are the practical applications of Runtime during development