On Apr 10, 2011, at 03:25, Philipp Leusmann wrote:

> This absolutely explains the observed behavior, but still leaves me wondering 
> about the feature of sending messages to id. 
> While I am still new to programming in objective-c and only having read the 
> basic language documentation, I remember this feature to be pointed out as a 
> helpful tool. But after this discovery, I can only mark it as a "don't use" 
> language feature.

The general rule is to avoid defining any method whose selector is the same as 
another method with the same selector but incompatible parameter/return types. 
That's not unexpected in a C-based language, because polymorphism can't work 
unless there are some rules about compatibility of calling/return conventions.

The case you ran into is an unfortunate corner case (which has caught lots of 
experienced Objective-C developers too), because in the architecture you were 
compiling for, sizeof (NSUInteger) == sizeof (CGFloat), and by default the 
compiler doesn't complain in that case, if the message is sent to a receiver of 
type 'id' in the source code method invocation.

On Apr 10, 2011, at 05:45, Philipp Leusmann wrote:

> In this situation XCode4 did not show a warning. 


There a couple of things you should do:

1. Turn on the "strict selector matching" compiler warning in your build 
settings.

2. Try to avoid naming your methods with names that seem likely to conflict 
with declarations already in the frameworks. Yes, I know this is ridiculous 
advice, because you can't be expected to know the name of every method that's 
already been declared, but in practical terms it's worth avoiding simple 
one-word method names like "width" or "size" or "name". The more specific your 
own method names are, the more likely you are to avoid clashes. Plus, it 
enhances your code's self-documentation characteristics! 

3. Pay attention to syntax coloring. In your preferences, make sure the 
"project" colors are different from the "other" colors. (They were by default 
in Xcode 3. I'm not sure they are different by default in Xcode 4).

In this case, if the color preferences were different, you would have noticed 
that the width/height invocations were the "wrong" color, and that would have 
been a clue to figure out why your own method signatures weren't being used.


_______________________________________________

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