Re: Multiple declarations for method length
Re: Multiple declarations for method length
- Subject: Re: Multiple declarations for method length
- From: "Louis C. Sacha" <email@hidden>
- Date: Tue, 30 Dec 2003 14:55:18 -0800
Hello...
The code that executes may always be determined at runtime, but the
type of the return value needs to be known at compile time. The
compiler warning is valid and indicates a problem that needs to be
corrected, because it can cause your code to work incorrectly.
The reason this warning is being produced is that the prototypes
(method signatures) for the two methods are different.
NSString's version is - (unsigned)length;
NSStatusItem's version is - (float)length;
The compiler needs to know which method prototype to use when
compiling the code, since using the wrong one will cause runtime
errors. The code it generates will be different depending on whether
the returned value is a float or an unsigned integer (and depending
on how that value is used in the future). This is not something that
can be determined at runtime, since the compiler needs to know what
type the value is.
There is more info about this in the pdf/html book about the
Objective C Language included with the Apple documentation (in the
section title "Enabling Static Behaviors" in the "Objective C
Language" chapter):
In MacOS_10.2 developer tools documentation
/Developer/Documentation/Cocoa/ObjectiveC/3objc_language_overview/Enabling_Static_Behaviors.html
or /Developer/Documentation/Cocoa/ObjectiveC/ObjC.pdf
In MacOS_10.3 developer tools documentation (why did they have to
rearrange everything ???)
/Developer/Documentation/Cocoa/Conceptual/ObjectiveC/3objc_language_overview/chapter_3_section_8.html
or /Developer/Documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf
The simple answer is as Daryn said: if myString is an NSString, then
[(NSString *)myString length]
is the way to fix the problem.
Hope that helps,
Louis
Ken Ferry <email@hidden> wrote:
On Dec 30, 2003, at 2:55 PM, Daryn wrote:
Is myString declared as an id? If so, the compiler doesn't know which
one you want, although the runtime will invoke the correct one. You
can eliminate the warning by either declaring or casting myString as a
NSString*.
This is actually not true. Objective-C is more dynamic that other
object oriented languages, and the code that executes is always
determined at runtime.
We say 'send messages' instead of 'call methods' in objective-c to
try to indicate the dynamism. You say to an object 'length' and it
interprets that message however it wants.
On Dec 30, 2003, at 1:20 PM, Jay Rimalrick wrote:
This is the warning I get when I compile my project.
For some reason it is getting confused between the NSStatusItem length
method and the NSString length method. Is there any way to explicitly
use the NSString length method in my code over and above my simple
[myString length];
thanks,
> jay
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.