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:
- SEL action = [button action];
We use a selector like this, and the following selector is called Action:
- [Foo Action]
- [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:
- -(void) Settarget: (ID) target;
- -(void) Setaction: (SEL) Action;
The following statement sets the action on a button object to "@selector (start:)", that is, it calls the Start method:
- [Button setaction: @selector (start:)];
If you have two parameters on your method, such as:
- -(void) SetName: (NSString *) name Age: (int.) Age;
So, your selector should write like this:
- 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:
- Sel sel = @selector (start:);//Specify Action
- if ([obj Respondstoselector:sel]) {//Determine if the object has a corresponding method
- [obj Performselector:sel withobject:self]; Call Selector method
- }
Let's look at an example of an app selector.
Example 2-16 Selector instance.
- #import <foundation/foundation.h>
- @interface Classa:nsobject {
- }
- -(void) print;
- @end
- @implementation ClassA
- -(void) print{
- NSLog (@ "I ' m ClassA.");
- }
- @end
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- Sel sel = @selector (print);
- ClassA *ClassA = [[ClassA alloc]init];
- [ClassA Performselector:sel Withobject:nil]; Invokes the method specified by the selector
- [Pool drain];
- return 0;
- }
"Program Results"
- 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:
- -(void) print{
- NSLog (@ "I ' m ClassA.");
- }
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.
- 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.
- ClassA *ClassA = [[ClassA alloc]init];
- [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)