Measure the test taker's understanding about the basic concepts and operations of Selector in Objective-C.

Source: Internet
Author: User

UnderstandingObjective-CMediumSelectorThe basic concepts and operations are what we will introduce in this article. In the iphone program, we will see a lot of usage such as @ selector. <Iphone Development Foundation> it took a lot of space to parse this syntax, but I don't know whether it is a translation problem or a too detailed explanation. I don't know much about it, but it is very difficult. until the program uses this syntax and carefully reads some parsing articles. To understand this syntax.

In short, you can understand @Selector() Is to take the class method number, his behavior 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, andObjective-CThe class cannot directly apply the function pointer, so only one @SelectorSyntax.

The result is a SEL type. Is this type essentially the number (function address) of the class method )? Therefore, we have the following code.

1. Obtain the selector value.

C function pointer

 
 
  1. Int add (int val)
  2. {
  3. Return val + 1;
  4. }
  5.  
  6.  
  7. Int (* c_func) (int val); // defines a function pointer variable.
  8. _ Func = add; // assign the addr address of the function to c_func.

Objective-C selector,

 
 
  1. @ Interface foo
  2. -(Int) add: int val;
  3. @ End
  4.  
  5.  
  6. SEL class_func; // defines a class method pointer
  7. 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. You can use a string to find the method SEL variable name = NSSelectorFromString (the string of the method name );

Note 4. You can use the SEL variable to reverse query the method name string.

 
 
  1. NSString * variable name = NSStringFromSelector (SEL parameter );

2. Execute the selector value.

After obtaining the matching value, how can we handle the SEL value? This is still the same as the function pointer, that is, executing it.

Function pointer execution. There are several equivalent forms below)

 
 
  1. *c_func(10);  
  2. c_func(10); 

SEL variable execution. Use the performSelecor Method for execution.

 
 
  1. [Object: withObject: parameter 1 withObject: parameter 2];

3. Application scenarios of selector

SelectorEssentially, it is the same as the callback function of C. It is mainly used for loosely coupled communication between two objects. This method is used in many development environments. For example, GTK and Delphi. Basically, the object between the entire Cocoa database and communication between controls are built on this basis.

Summary: UnderstandingObjective-CMediumSelectorThe basic concepts and operations are described in this article. I hope this article will help you!

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.