On 2009/09/07, at 13:11, Graham Cox wrote:


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.

UISlider does not have a - floatValue method.


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).

I don't get this. Won't the call

[sender value]

be dealt by objc_msgSend? Will it not traverse the isa pointer to get the method table and find the method named "value" and call that? At least from the UISlider class hierarchy there seems to be only one method named "value", no ambiguity there.


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.

I know this. I just want to fully understand why this is happening for this particular name "value" and not for "maximumValue".


--Graham



Paulo F. Andrade

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to