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 when I ran ps ax that PID could not be found. What does that integer represent and why do we have to use @selector all the time instead of just passing the method name?
@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.
I think looking at the Objective-C implementation might be good for an understanding:
A selector is an integer value. But its type is different from normal C integer values so you can't assign them.
The selector name like "methodName" is the string that uniquely represents a name for this integer.
Other languages and systems call this unique program wide string to integer mapping an atom (Windows) or quark (GTK).
Objective-C keeps all functions for a class inside a hashtable. The hashtable key is an integer. The Objective-C runtime library looks up the hashtable on every method invocation. Without the unique integer number it would be much slower to do this critical lookup.
A selector is not an opaque pointer to a structure anymore. With MacOSX 10.6 the obj_send runtime function which implements the Objective-C method invocation is using an arithmetic operation on the selector at the beginning to find out if it is a retain, release, autorelease message and do something in this special cases. For example simply return in case you are using the garbage collector.