Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multiple declarations for method `length' ?




On Dec 30, 2004, at 1:36 PM, John Stiles wrote:

On 2004-12-30, at 19.05, Mark Dawson wrote:

When compiling this Obj-C line in XCode 1.5, I get the following warning:

LengthParser.m:295: warning: multiple declarations for method `length'

short decimalLen = [[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator] length];


What does this mean? It seems to run OK, producing the result (1 for US system) that I'd expect.

And Joar replies:

If you check NSDictionary you'll find that:

- (id)objectForKey:(id)aKey

...it returns an "id" object. In other words, the compiler don't know what type of object it is. When you call the method "length" on that object, the compiler needs to guess what implementation to use. If there is only one such method it's easy, but if there are several it's not. You can help the compiler by casting [...]

I thought one of the huge advantages of message-passing over function calls was that you could send messages to "id" objects and objc_send would sort out the details.

Yes, which is why it works. But the compiler, which is issuing the warning, can't necessarily tell what value will receive the message at runtime, and so it's warning you that there are two possible methods that might be invoked depending on what class the object is. If you want to get rid of the warning, you can make your intent clear to the compiler by inserting a cast.


Charlton


-- Charlton Wilbur email@hidden email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >multiple declarations for method `length' ? (From: Mark Dawson <email@hidden>)
 >Re: multiple declarations for method `length' ? (From: j o a r <email@hidden>)
 >Re: multiple declarations for method `length' ? (From: John Stiles <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.