Function call
Objective-C is a dynamic language. A function is composed of a selector (SEL) and an implement (IML. Selector is equivalent to the house number, and Implement is the real household (function implementation ).
Like in real life, a house card can be randomly issued (@ selector (XXX), but not all of them can be found by a household. If the House cannot be found, the system will give the program several chances to run normally, if there is no way out, an exception will be thrown. It is the process of searching the IML of SEL when calling objc_msgSend. Let's take this process as an example to look at the useful functions involved in it.
Figure: Process for searching functions during running
ResolveInstanceMethod Function
Prototype:
+ (BOOL)resolveInstanceMethod:(SEL)name
This function is executed at runtime and when SEL's IML is not found.
According to the document, if function code is added, YES is returned, and NO is returned.
Example:
dynamicMethodIMP(+ (aSEL ==], aSEL, (IMP) dynamicMethodIMP,
According to the Demo experiment, the objc_msgSend function implemented by the BOOL value system returned by this function has no reference. No matter what system is returned, it will try to use SEL to find IML again. If function implementation is found, the function will be executed. If other search procedures cannot be found.
ForwardingTargetForSelector:
Prototype:
- ()forwardingTargetForSelector:(SEL)aSelector
The process is here ,.
The return parameter is an object. If this object is returned, the system forwards the running message to this object for execution. Otherwise, search for other processes.
Implementation example:
+(- (- (*none =
When the MyTestObject object executes the [myTestObject nonClassMethod] function, the message is thrown to the NoneClass object for execution.
MethodSignatureForSelector:
Prototype:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
This function and forwardInvocation are the last chance to find IML ..
ForwardInvocation:
Prototype:
- ()forwardInvocation:(NSInvocation *)anInvocation
Actually execute from methodSignatureForSelector: returned NSMethodSignature. In this function, NSInvocation can be repeatedly forwarded to multiple objects, which is also a flexible way. (ForwardingTargetForSelector can only be switched to an object in the form of Selector)
The following sample code illustrates the advantages of this implementation:
<Foundation/Foundation.h> *@property (retain) NSString ** - ( ((self == forKey: forKey:- (- (NSMethodSignature **sel = ([sel rangeOfString:].location == [NSMethodSignature signatureWithObjCTypes: [NSMethodSignature signatureWithObjCTypes:- ()forwardInvocation:(NSInvocation * NSString *key = ([key rangeOfString:].location == key = [[key substringWithRange:NSMakeRange(, [key length]-* [invocation getArgument:&obj atIndex: NSString *obj =&
DoesNotRecognizeSelector:
Prototype:
- ()doesNotRecognizeSelector:(SEL)aSelector
As the final step of function implementation, NSObject has only one function to implement, that is, throwing an exception.
Although theoretically this function can be reloaded to ensure that no exception is thrown (super implementation is not called), the Apple document emphasizes that "this function must not be ended and an exception must be thrown ".
Use Cases
When a function cannot be found, Objective-C provides three methods to remedy the problem:
1. Call resolveInstanceMethod to give a chance to add this function.
2. Call forwardingTargetForSelector to execute this function.
3. Call methodSignatureForSelector and forwardInvocation to flexibly execute the target function in other forms.
If none of them are in progress, an exception is thrown when doesNotRecognizeSelector is called.
Reference
Https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/
Https://www.mikeash.com/pyblog/friday-qa-2009-03-27-objective-c-message-forwarding.html
Http://blog.csdn.net/yiyaaixuexi/article/details/8970734