Day11 oc SEL

CELL

1. Wrap the test method into sel type data

2. Find the corresponding method address according to the sel data

3. Find the corresponding method according to the method address

Person *p = [[Person alloc] init];
//Indirectly call the method through sel [p performSelector:@selector(test)];
[p test];
//传参[p performSelector:@selector(test1:) withObject:@"aaa"]
[p test1:@"aaa"];
  •  method storage location

The method list of each class is stored in the class object, and each method has a corresponding sel type object. According to the sel object, the address of the method can be found and then called.

  • Definition of type sel

typedef struct objc_selector *SEL;

  • Creation of sel objects

SEL s1 = @selector(test);

SEL s2 = NSSelectorFromString(@"test");

  • There is a _cmd (SEL) inside each method, representing the current method

NSString *str = NSStringFromSelector(_cmd);//Print the current method name

[self performSelector:_cmd];//Calling in the method will cause an infinite loop

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326917993&siteId=291194637