A summary of the SEL and selector principles of "iOS development"

Source: Internet
Author: User
introduction of Selector (selector) A selector is a name used to select a method to execute for an object, or to replace the name of a unique identifier for that name when compiling the source code. A selector does not do anything by itself. It simply identifies a method. The compiler ensures that the selector is unique, except that the method name of the selector is different from the normal string. What makes a selector useful is (at runtime) it is like a dynamic function pointer , which automatically points to a method's implementation for a given name and applies to any class it uses. Suppose you have a Run method selector and a class dog, Student. The selector can use an instance of each class to invoke its running method, even if the implementation of each class may be different. second, get a selector The type of the compiled selector is the SEL. There are two common ways to get selectors: (1) When compiling, the compiler instruction @selector to obtain.

SEL aselector = @selector (methodname); (2) at runtime, the string is used to get a method name nsselectorfromstring

SEL aselector = nsselectorfromstring (@ "methodname"); iii. use of selector Use the selector that you have created. You can invoke a method by Performselector:

Create a Run method selector
SEL aselector = @selector (run);

//through Performselector: To invoke the run method
[Adog Performselector:aselector] of the object;
[Anstudent Performselector:aselector]; Note: A selector is typically created on its own under special circumstances, such as when you expose a button's click event to the outside class to implement its click Method,-(void) Addtarget: (ID) Target action: (SEL) Action; This is where @selector is used.

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.