What actually is a @selector?

后端 未结 2 565
情深已故
情深已故 2020-12-14 12:16

There are some functions which take as an argument @selector(methodName). I used NSLog to find out what @selector is, and it returns an integer. It looks like a PID, but whe

2条回答
  •  心在旅途
    2020-12-14 12:44

    @selector() is a compiler directive to turn whatever's inside the parenthesis into a SEL. A SEL is a type to indicate a method name, but not the method implementation. (For that you'd need a different type, probably an IMP or a Method) Under-the-hood, a SEL is implemented as a char*, although relying on that behavior is not a good idea. If you want to inspect what SEL you have, the best way to do it is to turn it into an NSString* like this:

    NSLog(@"the current method is: %@", NSStringFromSelector(_cmd));
    

    (Assuming you know that _cmd is one of the hidden parameters of every method call, and is the SEL that corresponds to the current method)

    The Objective-C Programming Language Guide has much more information on the subject.

提交回复
热议问题