【问题标题】:What's the difference between the method signature and the selector in Objective-C?Objective-C 中的方法签名和选择器有什么区别?
【发布时间】:2010-06-22 16:19:07
【问题描述】:

到目前为止,我认为-(void)startToDoSomethingWithThis:(That*)thing andThat:(That*)otherThing具有以下“方法签名”,同时也是选择器:-startToDoSomethingWithThis:andThat:

但是现在有人说选择器不像方法签名,方法签名还包含参数及其类型。对吗?

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    选择器是类中方法的名称。它用于标识方法,最常见的是在调用它时。签名是对参数和返回类型的描述。它在调用任意方法时使用,例如通过 NSInvocation 来安排参数并为返回值腾出空间。许多选择器可能具有相同的签名。

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

    NSMethodSignature 是 objc_method_description 类型的包装器。

    【讨论】:

    • 那么“方法签名”是否包含参数及其类型以及方法的返回类型?
    【解决方案2】:

    没错。选择器是方法名称。方法签名是返回类型和参数类型的封装。您可以使用+[NSObject instanceMethodForSelector:] 内省方法签名,它返回一个NSMethodSignature 对象。

    【讨论】:

    • NSMethodSignature 不记录与其相关的选择器。
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多