iOS develops a simple summary of SEL

Source: Internet
Author: User

SEL is a wrapper for the method. Package SEL type data it corresponds to the corresponding method address and can be called by locating the method address. The methods of each class in memory are stored in the class object, each method has a corresponding SEL type of data, according to a SEL data can find the corresponding method address, and then call the method.
    1. @interface Person:nsobject
    2. + (void) test1;
    3. -(void) test2;
    4. @end
    5. //According to the person class and method defined in the. h file, this line of code is executed in memory as follows
    6. Person *person = [[Person alloc] init];

SEL is a wrapper for the method. Package SEL type data it corresponds to the corresponding method address, find the method address to call the method

1. Where to store the method

    • The methods of each class in memory are stored in the class object
    • Each method has a corresponding SEL type of data
    • The corresponding method address can be found based on a SEL data, which in turn calls the method
    • Definition of SEL type: typedef struct OBJC_SELECTOR *sel

Creation of 2.SEL objects

    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

Other uses of 3.SEL objects

    1. Convert a Sel object to a NSString object
    2. NSString *str = Nsstringfromselector (@selector (test));
    3. Person *p = [person new];
    4. Call the test method of the object P
    5. [P Performselector: @selector (test)];
  1. /******************************* Person.h File **********************************/
  2. #import <Foundation/Foundation.h>
  3. @interface Person:nsobject
  4. -(void) test1;
  5. -(void) Test2: (NSString *) str;
  6. @end
  7. /******************************* person.m File **********************************/
  8. #import "Person.h"
  9. @implementation person
  10. -(void) test1
  11. {
  12. NSLog (@"non-Parametric object method");
  13. }
  14. -(void) Test2: (NSString *) str
  15. {
  16. NSLog (@"method%@ with Parameters", str);
  17. }
  18. @end
  19. /******************************* main.m File **********************************/
  20. #import "Person.h"
  21. #import <Foundation/Foundation.h>
  22. /*
  23. There are two ways to invoke a method:
  24. 1. Calling directly from the method name
  25. 2. Indirectly through the SEL data to invoke
  26. */
  27. int main (int argc, const char * argv[])
  28. {
  29. Person *person = [[Person alloc] init];
  30. //1. When executing this line of code, the Test2 is packaged into SEL type data
  31. //2. Then find the corresponding method address according to the SEL data (compare consumption performance but the system will have cache)
  32. //3. Call the corresponding method according to the method address
  33. [Person test1];
  34. //The method is packaged directly into the SEL data type to invoke Withobject: Incoming parameters
  35. [Person Performselector: @selector (test1)];
  36. [Person Performselector: @selector (test2:) withobject:@"incoming parameters"];
  37. return 0;
  38. }

"Editor's recommendation"

    1. How Android and iOS defend against malicious apps
    2. Baidu Mobile interview iOS question sharing
    3. iOS7.1 official release: Added CarPlay car function adjustment part UI and bug
    4. Research shows that developers are more interested in HTML5 than iOS?
    5. iOS basic memory management: Autorelease and Autoreleasepool

iOS develops a simple summary of SEL

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.