) Selector concept in object-C

Source: Internet
Author: User
Original Author Andrew Huang <Bluedrum@163.com> reprinted please indicate the author @ Selector () can basically be equivalent to the function pointer in C language, but in C language, the function name can be directly assigned to a function pointer, and the object-C class cannot directly apply the function pointer, in this way, only one @ selector syntax can be used. The result isSelType. Is this type essentially the number (function address) of the class method )? Therefore, we have the followingCode.
1. Obtain the selector value.
C function pointer Int add (INT Val) { Return Val + 1; }
INT (* c_func) (INT Val); // defines a function pointer variable. C_func = add;// Assign the ADDR address of the function to c_func.
Object-C Selector @ Interface foo -(INT) Add: int val; @ End
Sel class_func; // defines a class method pointer Class_func = @ selector (add: INT);
Note 1. @ selector is the method to find the current class, and [object @ selector (method name: method parameter...)]; is the celebration method of the class corresponding to the object. NOTE 2: When you look up a class method, in addition to the method name, the method parameters are also one of the query conditions. Note 3. Strings can be used. Method Sel variable name = nsselectorfromstring (method name string ); Note 4. You can use the SEL variable to reverse check during running. Method Name string Nsstring * variable name = nsstringfromselector (SEL parameter ); 2. Execute the selector value. After obtaining the corresponding value, how can we handle the SEL value? This is still the same as the function pointer, that is, executing it. Function pointer execution (the following are equivalent forms) * C_func (10 ); C_func (10 );

Sel variable execution. Use the performselecor Method for execution. [Object: withobject: parameter 1 withobject: parameter 2];
3. Application scenarios of Selector 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.

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.