Ken Thomases wrote:
On Mar 23, 2008, at 8:18 PM, Tron Thomas wrote:

At this point, the compiler only knows that object is an id. It doesn't know the specific class. So, of any of the "value" methods it might know about, it doesn't know which specific one is meant. Normally that wouldn't matter, but when you're dealing with float returns (or struct returns) the generated code would be different.

In other words, the compiler is generating the code here as though it were invoking a "value" method that returns an int. What is actually being invoked is a method which returns a float. The mismatch causes the problem. The problem is even though the value is updated to some non-zero value, the observed value is always zero. I have tried stepping through the code and verified that when I step into [AnotherObject value] method from the call in [SomeObject observeValueForKeyPath:ofObject:change:context:], the _value instance member is non-zero. Yet when I return to the calling method, the local value variable gets set to zero. This does not make sense.

What would allow the SomeObject instance to get the proper value for AnotherObject when observing it?

There are two approaches to fixing this:

1) Use a method name which won't be ambiguous about its return type, such as floatValue. It's not the name that actually matters, it's that all of the "floatValue" methods that the compiler might know about at that point have a return type of float -- because what perverse soul would create a floatValue method that returned something other than float -- so there's no chance of confusion.

2) Cast "object" to the specific class that you know it to be (i.e. AnotherObject*). You would have to make sure you've imported AnotherObject.h, of course.

Cheers,
Ken


Using a type cast solved the problem.  Thanks Ken.

_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [EMAIL PROTECTED]

Reply via email to