English 中文(简体)
谁能根据 Apple果-C文件中的这一节选解释挑选者?
原标题:Can anyone explain selectors according to this excerpt from the Apple Objective-C documentation?

根据 Apple果目标C文件,粗略相关部分:

www.un.org/Depts/DGACM/index_spanish.htm 方法回归和参数类型

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.

我已经读过这个栏目,但我看不出似乎有矛盾之处。 首先,它指出,所有采用相同名称的方法,都必须具有相同的返回类型和参数类型,因为动态具有约束力。

既然所有方法都与相同的选择一样对待,这是否意味着,无论我拥有多少不同的物体,如果他们都有一种EatCake(EatCake)方法,那么他们都会分享同样的选择。 如果是的话,那么他们为什么有同样的参数和返回类型?

然后,在下一个部分,它说,虽然他们是由同一选择人代表的,但他们可以有不同的参数类型和返回类型。 因此,我现在完全混淆不清,我认为刚才所说的情况并非如此。

我并不期望这是错误的,我希望我根本不理解这两个发言之间的区别。

谁能向我表明这一点?

最佳回答

并不要求所有拥有相同选择权的方法都有相同的参数和返回类型。 选择人只是一个名称,没有附加任何信息,可以确定方法。

问题在于,汇编者必须知道,当你称之为一种方法时,参数和回归类型是什么,以便它能够进行对你的检查。 当有关动态接收器的节选会谈时,它谈论的是带有一种类型的<代码>id的变数和通过一种回收方法发送的信息id。 由于这只告诉汇编者,它是一个目标,而不是它属于哪个类别,因此无法确定哪个类别应当用来确定参数和返回类型。 因此,它能够知道的唯一途径是,选择人的所有用途是否都具有相同的参数和返回类型。

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 an NSString 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.

问题回答

Armed with the following references:

我得出了以下令人惊讶的结论(我!) ,即不可能有两种方法,即完全相同的选择,但不同的参数/回报类型。 http://www.ohchr.org。

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.





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签