IOS Selector (Selector)

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.