Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the method signature and the selector in Objective-C?

Tags:

objective-c

Until now, I believed that -(void)startToDoSomethingWithThis:(That*)thing andThat:(That*)otherThing has the following "method signature", which is at the same time also the selector: -startToDoSomethingWithThis:andThat:

But now someone said that the selector is not like the method signature, and that the method signature also contains the arguments and their types. Is that correct?

like image 306
dontWatchMyProfile Avatar asked Dec 12 '22 21:12

dontWatchMyProfile


2 Answers

A selector is the name of a method within a class. It is used to identify the method, most often when it is being called. A signature is a description of argument and return types. It is used when calling an arbitrary method, for example by NSInvocation, to arrange arguments and make room for the return value. Many selectors may have the same signature.

SEL aSelector = @selector(method:foo:);
NSMethodSignature *aSignature = [theObject methodSignatureForSelector:aSelector];

NSMethodSignature is a wrapper around objc_method_description types.

like image 121
drawnonward Avatar answered Jun 01 '23 16:06

drawnonward


That's correct. A selector is the method name. A method signature is an encapsulation of the return type and argument types. You can introspect method signatures using +[NSObject instanceMethodForSelector:], which returns an NSMethodSignature object.

like image 28
Dave DeLong Avatar answered Jun 01 '23 17:06

Dave DeLong