[Objective-C] sel type, @ selector Selector

Source: Internet
Author: User

1 ID cattle [3];
2 sel say;
3 sel skin;

The ID cattle [3] defines an array used to store cattle or bull objects. This line of code is probably quite familiar to everyone, so I will not go into details here. Traditional arrays like this cannot fully meet our needs. It is inconvenient for us to perform operations such as append and delete. In the subsequent sections, I will introduce nsarray, an alternative solution to traditional arrays.

The second and third lines of the previous Code are concerned in this section, that is, the SEL type. During compilation, objective-C generates
To distinguish the unique ID of this method. This ID is of the SEL type. We need to note that as long as the method names (including parameter sequences) are the same, their IDs are the same. That is to say, no matter whether it is a super class or a subclass, no matter whether there is a relationship between a super class and a subclass, the ID is the same as long as the name is the same. In addition to the function name and ID, the compiler also needs to compile the method into code that the machine can execute. In this way, in a compiled class, the table of the method shown in is generated (this structure is speculative by the author and has not been officially confirmed. Therefore, Figure 5-2 is for reference only, and we can temporarily consider it as this ).


Figure 5-2, method table

Note that setskincolor is followed by a colon because it contains parameters. Because such a table exists, during program execution, we can conveniently obtain the method ID through the method name, that is, the Sel We call, and vice versa. The usage is as follows:

1 sel variable name = @ selector (method name );
2 sel variable name = nsselectorfromstring (method name string );
3 nsstring * variable name = nsstringfromselector (SEL parameter );

The first line is the name of the method directly written in the program, the second line is the string that writes the method name, and the second line is the name of the method obtained through the SEL variable. After obtaining the SEL variable, we can send a message to an object through the following call:

[Object: withobject: parameter 1 withobject: parameter 2];

This mechanism greatly increases the flexibility of our program. We can pass the SEL parameter to a method so that this method can dynamically execute a method; you can also specify the method to be executed through the configuration file. After the program reads the configuration file, it translates the method string into a sel variable and sends the message to the corresponding object.

In terms of efficiency, the method ID, or an integer, is not used to find the method during execution. Because the search and matching of integers are much faster than strings, therefore, the execution efficiency can be improved to some extent.

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.