Summary of SEL in iOS development

Source: Internet
Author: User

 
 
  1. @ Interface Person: NSObject
  2.  
  3. + (Void) test1;
  4. -(Void) test2;
  5. @ End
  6.  
  7. // Execute this line of code based on the Person class and method defined in the. h file as follows in the memory
  8. Person * person = [[Person alloc] init];

SEL is a method packaging. The encapsulated SEL data corresponds to the corresponding method address. You can call the method by finding the method address.

1. storage location of the Method

  • In memory, methods of each class are stored in class objects.
  • Each method has a SEL-type data corresponding to it.
  • You can find the corresponding method address based on a SEL data and call the method.
  • SEL Type Definition: typedef struct objc_selector * SEL

2. Create a SEL object

 
 
  1. SEL s1 = @ selector (test1); // wrap the test1 method into a SEL object
  2. SEL s2 = NSSelectorFromString (@ "test1"); // convert a string method to a SEL object

3. Other SEL object usage

 
 
  1. // Convert SEL object to NSString object
  2. NSString * str = NSStringFromSelector (@ selector (test ));
  3.  
  4. Person * p = [Person new];
  5.  
  6. // Call the test method of object p
  7. [P performSelector: @ selector (test)];
 
 
  1. /******************************* Person. h file **********************************/
  2.  
  3. # Import <Foundation/Foundation. h>
  4.  
  5. @ Interface Person: NSObject
  6.  
  7. -(Void) test1;
  8.  
  9. -(Void) test2 :( NSString *) str;
  10.  
  11. @ End
  12.  
  13. /******************************* Person. m file **********************************/
  14.  
  15. # Import "Person. h"
  16.  
  17. @ Implementation Person
  18.  
  19. -(Void) test1
  20. {
  21. NSLog (@ "Object method without Parameters ");
  22. }
  23.  
  24. -(Void) test2 :( NSString *) str
  25. {
  26. NSLog (@ "method with parameters % @", str );
  27. }
  28. @ End
  29.  
  30. /******************************* Main. m file **********************************/
  31.  
  32. # Import "Person. h"
  33. # Import <Foundation/Foundation. h>
  34.  
  35. /*
  36. There are two ways to call a method:
  37. 1. Call the method name directly.
  38. 2. Indirectly calling through SEL data
  39. */
  40.  
  41. Int main (int argc, const char * argv [])
  42. {
  43. Person * person = [[Person alloc] init];
  44. // 1. When this line of code is executed, test2 will be packaged into SEL-type data
  45. // 2. Find the corresponding method address based on the SEL data (performance consumption is compared, but the system will cache)
  46. // 3. Call the corresponding method based on the method address
  47. [Person test1];
  48. // Wrap the method directly into the SEL data type to call withObject: input parameter
  49. [Person primary mselector: @ selector (test1)];
  50. [Person required mselector: @ selector (test2 :) withObject: @ "Incoming parameter"];
  51. Return 0;
  52. }

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.