Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.
runtime
OC
What do you mean by dynamic runtime language?
- Dynamic typing: The runtime determines the type of the object, which can pass at compile time, but does not mean there are no problems at run time
- Dynamic binding: Methods invoked by objects are determined at runtime (message forwarding)
- Dynamic loading: the implementation of a dynamic library method is not copied into the program, only the reference is recorded, until the relevant method is used to find the implementation in the library
runtime
What can be done?
- Gets the class’s member variables, methods, and protocols
- Add member variables, methods, and protocols to the class
- Dynamic change method implementation
class_copyIvarList
withclass_copyPropertyList
The difference between?
- 1.
class_copyIvarList
You can get.h
and.m
All properties in the@interface
Variables declared in braces and property names retrieved are underlined (except in braces). - 2,
class_copyPropertyList
Can only be obtained by@property
Declared properties (including.m
), the property name obtained is not underlined.
class_ro_t
andclass_rw_t
The difference between?
class_rw_t
Provides the ability to extend classes at runtime,class_rw_t
Stored in the structureclass_ro_t
.class_ro_t
It stores information that has been determined by the class at compile time and is immutable.- Both contain information about the class’s methods, attributes (member variables), protocols, and so on, but the lists that store them are implemented differently. In simple terms
class_rw_t
Store the two-dimensional array used by the list,class_ro_t
Use a one-dimensional array. - Methods, properties, protocols, and so on that modify classes at run time are stored in
class_rw_t
In the
What is Method Swizzle and when is it used?
Method Swizzle
Is to change an existing selector (SEL
) corresponding implementation (IMP
).- A list of class methods is stored
SEL
The name andIMP
The mapping relation of. - Developers can leverage
method_exchangeImplementations
To swap the two methodsIMP
- Developers can leverage
method_setImplementation
To set the IMP of a method directly - This can be changed at run time
SEL
andIMP
To achieve method substitution.
Method Swizzle
Matters needing attention
- In order to ensure that
Swizzle Method
Method substitution must be performed on the call, which can be performed inload
Performed in the +load
Do not call when used inside[super load]
. If I call it multiple times[super load]
, may appear “Swizzle ineffective” illusion- Avoid calling
[super load]
Lead toSwizzling
Multiple executions, inload
The use ofdispatch_once
Ensure that the exchange is performed only once. - Subclasses that replace unimplemented inheritance methods can replace the implementation in the parent class, affecting the parent class and other children
+initialize
Inside use to adddispatch_once
- Some checks need to be done during version iteration in case the library functions change
How tohook
Methods of one object without affecting other objects
- Method 1: Create a new subclass override method
- Method 2: Make this object’s class follow a protocol,
hook
When the judgment. The drawback is that other objects that follow this agreement will suffer. - Method 3: Create a new subclass at run time and modify the object
isa
Pointers to subclasses,hook
The use ofisKindOf
Determine type
Message is sent
Message mechanism
- 1, fast search, method cache
- 2, slow search, method list
- 3. Message forwarding
- 3-1. Dynamic analysis of methods,
resolveInstanceMethod
- 3-2. Fast message forwarding
forwardingTargetForSelector
- 3-3. Standard message forwarding,
methodSignatureForSelector & forwardInvocation
- 3-1. Dynamic analysis of methods,
What happens when you send a message to a nil object in objC?
- Looking for someone
isa
Pointer returns the address0x0
, do not do any operations, there will be no errors.
What happens when OBJC sends a message to an object?
- A method call actually sends a message through the call
objc_msgSend()
The implementation. - First of all, through
obj
theisa
Pointer finds the correspondingclass
. - Then, start the quick lookup process. in
class
List of cache methods (objc_cache
) to find the method, if found directly return the correspondingIMP
. - If it is not found in the cache, the slow lookup process begins. in
class
theMethod List
Find the corresponding method, find the return correspondingIMP
. - If they can’t find it, they go through the message forwarding process
_objc_msgForward
What does a function do?
_objc_msgForward
Used for message forwarding: called when a message is sent to an object, but it is not implemented_objc_msgForward
Try to do message forwarding.
Why do I need to do method caching?
- Look it up every time you execute this method
Method List
Too much performance. - use
objc_cache
Make a cache of the methods that have been calledmethod_name
As akey
.method_IMP
As avalue
. - Next time you receive a message, just pass
objc_cache
To find the correspondingIMP
Just avoid going through it every timeobjc_method_list
What if I can’t find a way all the time?
- It triggers a message forwarding mechanism, and we have three chances to fix it to prevent it
crash
- The dynamic resolution of the method, through
resolveInstanceMethod
Add an IMP to make it run. - Fast message forwarding, in
forwardingTargetForSelector
Returns an object that can execute the method. - Standard message forwarding,
methodSignatureForSelector
Create a method signature of the same method type (NSMethodSignature
), and rewrite itforwardInvocation
And assigns the method that owns the signature toanInvocation.selector
.
The pros and cons of message forwarding mechanisms
- Advantages: The message forwarding mechanism provides the opportunity for a remedy when no method can be found.
- Disadvantages: Generally, crash processing is performed in the base class, so some crashes may be ignored and the problem cannot be exposed.
IMP
,SEL
,Method
The differences and usage scenarios
SEL
A code name used to find a method, handle notifications/timers, etcIMP
Is a pointer to the implementation of a method, used for dynamic method resolutionMethod
It’s an object. It’s in thereSEL
andIMP
Is used when the message forwarding process obtains the method signature