Re: multiple methods named 'xxx' found.
Re: multiple methods named 'xxx' found.
- Subject: Re: multiple methods named 'xxx' found.
- From: "Daniel T. Staal" <email@hidden>
- Date: Fri, 5 Oct 2007 09:37:13 -0400 (EDT)
- Importance: Normal
On Fri, October 5, 2007 8:18 am, Nelson Santos said:
> Hi Charles,
>
> Thanks for the reply. Well, that's the part I don't get. Isn't the
> compiler smart enough to choose the appropriate method based on which
> object I am sending the message to? If I am calling a method that is
> defined in the class of the object I am sending the message to, why would
> the compiler even think that I intend to call a completely unrelated
> class's method on my object? How is that even possible if that is
> actually what I intended to do?
Actually it is smart enough, the problem is that you the call doesn't
specify the object type that you are sending the message to. If you _did_
specify, then it wouldn't be a problem, but in this case it is very hard
to specify.
Let's take a look at that snippet of code again:
[[HMCharacterAlphabet alloc] initWithSize: 3]
You see it as 'Tell the HMCharacterAlphabet class to alloc a new instance,
and init it with size 3'. That's not quite what the compiler sees. It
sees 'Tell HMCharacterAlphabet to alloc a new instance, and tell the
result of that to init itself with size 3.'
Now, what is the result of [HMCharacterAlphabet alloc]? Well, I'm
guessing you didn't write that method for that class... (Please _don't_,
by the way.) So, who does it inherit it from?
Go back up the inheritance tree, and you'll find that alloc is defined as
a method of NSObject. (Where else?) And what does it return? _id_ An
unknown object.
So the above snippet tells the compiler to send 'initWithSize:' to an
unknown object. Therefore the compiler needs to check _every possible
class_ to figure out which ones respond to that. Hopefully only one does,
and then it can say 'Oh, I guess you mean send it there.'
But if it finds two classes that can both respond to that method, it has
to give an error, saying 'What do you want me to do?'
There are obviously workarounds, once you know what is going on. After
all, all you need to do is define that 'id' as a member of some class for
the compiler to know what you want to do. And if you _really_ need to,
you can use them. Expect the next programmer who takes a look at your
code (and you'll count as 'the next programmer' in a month) to look at the
workaround to say 'That's ugly and useless', and remove it. (And break
the code therefore.)
Better to try to find a unique name for that method. Then people won't
look at it funny.
Daniel T. Staal
---------------------------------------------------------------
This email copyright the author. Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes. This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden