Re: Sending the "value" message to an object typed "id"
Re: Sending the "value" message to an object typed "id"
- Subject: Re: Sending the "value" message to an object typed "id"
- From: Graham Cox <email@hidden>
- Date: Mon, 7 Sep 2009 22:11:56 +1000
On 07/09/2009, at 9:36 PM, Paulo F. Andrade wrote:
So what's the deal with the method name "value" and ObjC runtime?
As an iPhone question this may be incorrect, but normally you use -
floatValue to get a, umm, float value.
I suspect what's occurring is that -value is defined in multiple
classes having different return types: maybe float in one case and id
in another, say. The compiler can't disambiguate a method based on
return type alone so compiles based solely on the first one it
encounters, which to all intents and purposes may as well be random.
It's a dirty little secret of Obj-C (or at least the current
compilers) and is a very real source of bugs (and sometimes quite
dangerous ones at that).
To avoid it, where possible do not use type id and always use explicit
types if you can. If you know your sender is a UISlider*, then cast to
that when messaging it with method names that can be ambiguous. Short
and vague method names like -value are fairly likely to be ambiguous.
--Graham
_______________________________________________
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