SEL in Objective-c (reprint)

Source: Internet
Author: User

SEL

In Objective-c, the SEL is a type of selector (selector). A selector is a pointer to a method, and the reader can simply understand that running the program here will execute the specified method, so you can define a selector:

    1. SEL action = [button action];

We use a selector like this, and the following selector is called Action:

    1. [Foo Action]
    2. [Bar Action]

In target-action mode (a common pattern in a Cocoa program): Target specifies a class, and the Action specifies a method. Setting an action on an object is done by the selector:

    1. -(void) Settarget: (ID) target;
    2. -(void) Setaction: (SEL) Action;

The following statement sets the action on a button object to "@selector (start:)", that is, it calls the Start method:

    1. [Button setaction: @selector (start:)];

If you have two parameters on your method, such as:

    1. -(void) SetName: (NSString *) name Age: (int.) Age;

So, your selector should write like this:

    1. Sel sel = @selector (setname:age:);

If the method does not exist, the application calling the method may abort abnormally. Therefore, you need to use the Respondstoselector method to determine if the object has a corresponding method, and use the Performselector:withobject: method to invoke the selector:

    1. Sel sel = @selector (start:);//Specify Action
    2. if ([obj Respondstoselector:sel]) {//Determine if the object has a corresponding method
    3. [obj Performselector:sel withobject:self]; Call Selector method
    4. }

Let's look at an example of an app selector.

Example 2-16 Selector instance.

  1. #import <foundation/foundation.h>
  2. @interface Classa:nsobject {
  3. }
  4. -(void) print;
  5. @end
  6. @implementation ClassA
  7. -(void) print{
  8. NSLog (@ "I ' m ClassA.");
  9. }
  10. @end
  11. int main (int argc, const char * argv[]) {
  12. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  13. Sel sel = @selector (print);
  14. ClassA *ClassA = [[ClassA alloc]init];
  15. [ClassA Performselector:sel Withobject:nil]; Invokes the method specified by the selector
  16. [Pool drain];
  17. return 0;
  18. }

"Program Results"

    1. I ' m ClassA.

The following explanation of this code, the reader may not understand, because so far did not tell the class-related knowledge. Readers only need to understand the use of selectors in the above examples, and the knowledge about classes will be elaborated in later chapters.

The code first creates a class named ClassA, which contains only one method, print. In the subsequent implementation file, we implemented this method:

    1. -(void) print{
    2. NSLog (@ "I ' m ClassA.");
    3. }

The reader is not difficult to see, this method is only printed to the console a sentence. In the next main method, a selector sel is defined, which points to a method called print. We don't know which class this method is, because the specific information is automatically judged by the system during the run.

    1. Sel sel = @selector (print);

Then an object is constructed (the reader does not have to stick to the syntax, we will elaborate in a later chapter) and call this object Performselector:withobject: the method.

    1. ClassA *ClassA = [[ClassA alloc]init];
    2. [ClassA Performselector:sel Withobject:nil];

At this time, the system will automatically call the ClassA object's Print method, and finally get the program running results.

The above article is reproduced in the csdn of a great God, follow-up I will be specific sample code to show

IOS QQ Group: 5883244

SEL in Objective-c (reprint)

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.