Function pointers of @ selector () and c/c ++ in objective-c, objectivecselector
First look at the code used in tomcat:
// Then start the animation. // place the image in animationImages and accept the array parameter self. tom. animationImages = arrayImage; // set the interval between 81 images. If there are multiple images, the playback time is slightly longer. Otherwise, the image is short. self. tom. animationDuration = arrayImage. count * 0.074; // set the number of repetitions self. tom. animationRepeatCount = 1; // start the animation [self. tom startAnimating]; // ends the animation. You should wait and clear [self. tom specified mselector: @ selector (setAnimationImages :) withObject: nil afterDelay: self. tom. animationDuration];
The last line of code delays the process of loading the set method. @ Selector (xxxx) is used to find the method named xxxx. That is to say, the B method of object a is called, which means the same as [a B]; but this is more dynamic. The returned type is SEL. If the parameter type is SEL, the value returned by @ selector (xxxx) is accepted.
Recall:
Objective-c syntax faster than SEL data type in (5)
The list of methods of each class is stored in the class object, and each method has a corresponding SEL type data. The method address can be found based on a SEL type data, then call the method. SEL type definition:
typedef struct objc_selector *SEL;
SEL is actually a method packaging. It encapsulates the method into a SEL type data and finds the corresponding method address. You can call the method by finding the method address. The actually sent message is SEL.
The specific structure of the objc_selector depends on whether it is a C string mapped to SEL in Mac OS X when GNU or Apple is used. It can be considered as the method name, it does not point to the specific method implementation (the IMP type is ).
All classes have the same selector as long as the method name is the same.
Read the official documentation;
- (id)performSelector:(SEL)aSelector
Send the specified message to the receiver, and the pie returns the execution result. That is to say, the perform (which means the execution itself) Selector: xxx is the method for executing the method named xxx, it is only indirect execution. The method is found through the sel parameter (return sel type using @ selector (method name.
Here we can see that @ selector (methodName) is very similar to the function pointer concept in c language.
In C language, function names can be directly assigned to a function pointer, while the OC class cannot directly apply the function pointer, so that only one @ selector syntax can be used. its return result is a SEL type. This type is essentially the number (function address) of the class method ).
Recall function pointer:
The function also has an address. The pointer to the Function Points to (saves) the address at the beginning of the function code and declares a function pointer, you must first declare the function type (return type and parameter type) It points to, declare the pointer to the function, the function pointer can be used as a parameter of another function, tell the second function to use which function.
Void ToUpper (char *); // declare the function first ...... Void (* p) (char *); // the pointer p is a pointer to the ToUpper function.
That is, (* p) is also a function, char * is its parameter, void is the return type, and the expression (* p) replaces the function name. Because * The operator priority is lower than (), brackets must be added -- (* p). If parentheses are omitted, the meaning changes:
Void * p (char *); // It indicates that p is a function and returns a null pointer type.
If the function pointer is declared, the function name can be used to represent the function address. Of course, the pointer can also be used. Function pointers are most commonly used as function parameters. you can only point to functions of the same type. You can use function pointers as function parameters or function names as parameters (function names as function addresses). Function arrays are not supported! But an array with function pointers
Look at oc again,Obtain the selector value.
// The selector @ interface method-(int) add :( int) val; @ endSEL fun; // equivalent to defining a method pointer fun = @ selector (add :);
Note;
You can use a string to find a method.
SEL variable name = NSSelectorFromString (method name string );
You can use the SEL variable to reverse query the method name string.
NSString * variable name = NSStringFromSelector (SEL parameter );
After obtaining the corresponding value, how to handle the SEL value is still the same as the function pointer, that is, executing it. The SEL variable is executed using the performSelecor object method.
[Object: withObject: parameter 1 withObject: parameter 2];
Application scenarios of selector
Is something that allows Objective-C to call Methods dynamically.
Is the dynamic post-binding technology of object-c.
Function access through strings
Selector is essentially the same as C's callback function.This method is mainly used for loosely coupled communication between two objects. Basically, the communication between the entire Cocoa database and the control is built on this basis.