So in the last article we kind of saw ns select from string, NSClassFromString, SEL in CTMetidor and this article is going to focus on response to selector, performSelector Nsmediator, NSMethodSignature, NSInvocation, NSInvocation

So before we get to performSelector, let me just say a little bit about RunTime

RunTime

What is RunTime?

  • RunTimeRuntime for short. OC is the run-time mechanism, that is, some mechanisms at run time, the most important of which is the message mechanism, message (method) passing, if the message (method) is not found in the object, forward

What can RunTime be used for?

  • throughRunTimeWe can have an object send messages (i.e., execute methods), swap methods (Method Swizzling), dynamically add methods, add attributes to classes, dictionary transform models, and so on
Person *p = [[person alloc] init]; // Call the object method [p eat]; Objc_msgSend (p, @selector(eat)); Person eat = Person eat = Person eat = Person eat [[Person class] eat]; Objc_msgSend ([Person class], @selector(eat));Copy the code

respondsToSelector

(BOOL)respondsToSelector:(SEL)aSelector;

Determines whether an object responds to this method. This method is usually used with performSelector to prevent crashes

performSelector

The main use of CTMetidor in RunTime is for objects to send messages

PerformSelector is essentially converted to objc_msgSend for implementation, and its internal implementation steps:

1, find obj’s class via isa pointer;
Select * from method list where class = ‘eat’;
3, If the class does not find eat, continue to find its superclass;
4. Once the function eat is found, implement IMP.

Take a look at the code for CTMetidor:

[target performSelector:action withObject:params];
Copy the code

Action SEL we got from NSClassFromString, target we got from NSClassFromString, The next step is to execute the action in target (Class) via the performSelector method.