开发者

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

Until now, I believed that -(void)startToDoSomethi开发者_StackOverflow中文版ngWithThis:(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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜