谁能根据 Apple Objective-C 文档的摘录来解释选择器?
来自 Apple Objective-C 文档,相关部分加粗:
方法返回和参数类型
消息传递例程只能访问方法实现 通过选择器,因此它使用相同的选择器处理所有方法 一样。它发现方法的返回类型以及方法的数据类型 它的参数来自选择器。 因此,除了发送的消息 对于静态类型的接收者,动态绑定需要所有 具有相同返回值的同名方法的实现 类型和相同的参数类型。(静态类型接收器是 此规则的例外,因为编译器可以了解该方法 从类类型实现。)
虽然类方法和实例方法名称相同 由相同的选择器表示,它们可以有不同的参数 类型和返回类型。
我已经一遍又一遍地阅读这个块,但我似乎无法克服看似矛盾的问题。首先,它表示由于动态绑定,同名方法的所有实现都必须具有相同的返回类型和参数类型。
由于它对待所有具有相同选择器的方法都是一样的,这是否意味着无论我有多少个不同的对象,如果它们都有一个 EatCake() 方法,那么它们都将共享相同的 EatCake 选择器?如果是这样,那么为什么它们必须具有相同的参数和返回类型?
然后在下一部分中,它说虽然它们由相同的选择器表示,但它们可以具有不同的参数类型和返回类型。所以现在我完全困惑了,我以为它只是说事实并非如此。
我不认为这是一个错误,我希望我只是不理解这两个陈述之间的区别。
谁能帮我解决这个问题吗?
From the Apple Objective-C documentation, bolded relevant parts:
Method Return and Parameter Types
The messaging routine has access to method implementations only
through selectors, so it treats all methods with the same selector
alike. It discovers the return type of a method, and the data types of
its parameters, from the selector. Therefore, except for messages sent
to statically typed receivers, dynamic binding requires all
implementations of identically named methods to have the same return
type and the same parameter types. (Statically typed receivers are an
exception to this rule because the compiler can learn about the method
implementation from the class type.)Although identically named class methods and instance methods are
represented by the same selector, they can have different parameter
types and return types.
I've read this block over and over but I can't seem to get past what seems to be a contradiction. First it says that all implementations of identically named methods are required to have the same return type and parameter types because of dynamic binding.
Since it treats all methods with the same selector alike, does this mean that no matter how many different objects I have, if they all have a EatCake() method then they will all share the same selector for EatCake? If so, then why must they have the same parameters and return type?
Then in the next part it says though they are represented by the same selector, they can have different parameter types and return types. So now I'm totally confused, I thought it just said this was not the case.
I do not expect that this is a mistake, I expect that I am simply not understanding what the difference is between these two statements.
Can anyone clear this up for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要求具有相同选择器的所有方法都具有相同的参数和返回类型。选择器只是一个标识方法的名称,没有附加任何信息。
问题在于,当您调用方法时,编译器必须知道参数和返回类型是什么,以便它可以为您执行类型检查。当摘录讨论动态接收器时,它讨论的是具有
id
类型的变量以及发送到返回id
的方法结果的消息。由于这仅告诉编译器它是一个对象,而不是它是什么类,因此它无法确定应该使用哪个类来确定参数和返回类型。因此,它知道的唯一方法是该选择器的所有使用是否都具有相同的参数和返回类型。摘录还解释了静态类型接收器的例外情况,这意味着您为变量类型指定了某个类,例如
NSString *myString
。由于编译器知道该对象必须是 NSString 对象,因此它知道使用该类的参数和返回类型,因此不同类的对象不需要相同。这一切对运行时完全没有影响。当您调用方法时,运行时会获取该对象的实际类,并使用它来查找要调用的正确实现。它不执行类型检查,因此它不关心参数和返回类型是什么。
It is not required that all methods with the same selector have the same parameter and return types. The selector is simply a name which identifies the method, without any of that information attached.
The problem is that the compiler has to know what the parameter and return types are when you call a method so that it can perform type checking for you. When the excerpt talks about dynamic receivers, it is talking about variables with a type of
id
and messages sent to the result of a method which returnsid
. Since this only tells the compiler that it is an object, but not what class it is, it cannot determine which class should be used to determine the parameter and return types. Therefore, the only way it can know is if all uses of that selector have the same parameter and return types.The excerpt also explains that the exception is for statically typed receivers, which means you specified a certain class for your variable type, such as
NSString *myString
. Since the compiler knows that the object must be anNSString
object, it knows to use the parameter and return types from that class, so it doesn't need to be the same for objects of a different class.This all has absolutely no effect on the runtime. When you call a method, the runtime gets that objects actual class and uses that to find the proper implementation to call. It performs no type checking, so it doesn't care what the parameter and return types are.
配备以下参考资料:
和经过实验的装饰,我得出了以下令人惊讶的(对我来说!)结论:不可能有两种方法具有完全相同的选择器但参数/返回类型不同。 这在全球范围内都是如此。
第二个陈述令人惊讶。我们知道 Objective C 中不能存在方法重载(“给定类的单个选择器只能有一种类型签名。"),因此它在本地有效。但也不能有两个不同的不相关类具有完全相同的选择器但不同:
我偷偷怀疑我的结论可能不完全正确,所以如果您更了解,请告诉。
Armed with the following references:
and a garnishing of experimentation, I have come to the following surprising (to me!) conclusion that there can be no two methods with the exact same selector but different parameter/return types. And this holds true globally.
The second statement is what is surprising. We know that there cannot be method overloading in Objective C ("A single selector for a given class can only have one type signature."), so it holds locally. But there also cannot be two different unrelated classes with the exact same selector but different:
I have a sneaking suspicion that my conclusion may not be entirely correct, so if you know better, please tell.