1 Preface
This article is an article about selectors (Selector), Selector can be understood as a method name, see below.
Original English: http://blog.csdn.net/developer_zhang/article/details/12188099
Reprint please specify the Source: Http://blog.csdn.net/developer_zhang 2 Details
Selector is the name of an object used to select the method to execute, or the only indication that the source code is used to replace the name when it is compiled. Selector himself could not do anything. It simply indicates a method. The only thing that makes the selector method name different from the normal string is that the compiler determines that the selectors is unique. What makes selector useful (in conjunction with runtime) is that it acts like a dynamic function pointer to an implementation of the applicable method used by the class, for an already given name. Let's say we have a selector of the Run method, and class Dog,athlete and Computersimulation (each class implements the Run method). Selector can be used by an instance of each class and invoke its run method-the implementation of the set method may be different. 2.1 How to get selector
The compiled selectors are of the SEL type. There are two ways of getting selector:
• At compile time, we use the compile Mark @selector
SEL aselector = @selector (methodname);
• At run time, we use the Nsselectorfromstring method, and the string is the method name:
SEL aselector = nsselectorfromstring (@ "methodname");
We use selector to create a string when we want our code to send a message that we know the name at run time. 2.2 How to use selector
We are able to invoke the Performselector: method with selector as parameters and other similar methods to use:
SEL aselector = @selector (run);
[Adog Performselector:aselector];
[Anathlete Performselector:aselector];
[Acomputersimulation Performselector:aselector];
(We use this technique in special cases, such as we implement an object that uses the Target-action design pattern.) Typically, we simply invoke the method directly. )
3 Conclusion
The above is all content, hope to be helpful to everybody.