• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Compile error: "multiple methods named '-initWithSize:' found"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compile error: "multiple methods named '-initWithSize:' found"


  • Subject: Re: Compile error: "multiple methods named '-initWithSize:' found"
  • From: Julian Wood <email@hidden>
  • Date: Fri, 11 Dec 2009 11:56:45 -0700

Thanks Bill, that explains it.

Another simple way to fix the issue was to cast it.

... = [(Square *)[Square alloc] initWithSize: ...];

But I think it is probably better, as you said, to come up with an init method name that is more descriptive, rather than reuse method names from other classes with different argumentation.

Cheers,

J


On 2009-12-09, at 1:01 AM, Bill Bumgarner wrote:

>
> On Dec 8, 2009, at 11:24 PM, Julian Wood wrote:
>
>> main.m: In function 'main':
>> main.m:7: warning: multiple methods named '-initWithSize:' found
>> /System/Library/Frameworks/AppKit.framework/Headers/NSImage.h:81: warning: using '-(id)initWithSize:(NSSize)aSize'
>> Square.h:4: warning: also found '-(Square *)initWithSize:(int)s'
>> main.m:7: error: incompatible type for argument 1 of 'initWithSize:'
>>
>> Why would those two methods clash? NSImage is not even included. Moreover, aren't we saying specifically to use initWithSize from Square? And finally, why does it think we are not using an int as a param?
>
> More likely than not, <Cocoa/Cocoa.h> is in your precompiled header or otherwise included.
>
> The two methods class because the argumentation is different.  Though you didn't paste it, I'd bet you have this line of code:
>
> ... = [[Square alloc] initWithSize: ...];
>
> If you look at the declaration of +alloc, it returns (id) -- a reference to an instance of any class.   Thus, when the compiler compiles the -initWithSize: call, it is [correctly] warning that it there might possibly be ambiguity.   In your case, the ambiguity may cause failures in that passing a struct and passing an (int) might require a different ABI -- a different layout of parameters on the stack and in registers depending on architecture.
>
> The way to fix this is to never name a method the same as another method, but have different argument types.  Not ever.  If you were to poke through the Cocoa APIs, you'd find that, save for a very very very few cases that are all bugs, the Mac OS X APIs avoid this issue.
>
> It is also useful to avoid the use of (id) as much as possible.  It isn't entirely unavoidable in that Objective-C doesn't support covariant declarations of methods.   For example, the +array method might return an NSArray or an NSMutableArray [conceptually anyway as the actual implementation returns something completely different] depending on which class's +array implementation you invoke.   Because Objective-C can't deal with:
>
> + (NSArray *) array;
>
> ... and this in a subclass ...
>
> + (NSMutableArrayy *) array;
>
> ... the +array method is declared as returning the generic (id) so as to avoid required typecasting or annoying warnings.
>
> (Yes, there is a bug tracking this particular issue.)
>
> b.bum

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Compile error: "multiple methods named '-initWithSize:' found" (From: Julian Wood <email@hidden>)
 >Re: Compile error: "multiple methods named '-initWithSize:' found" (From: Bill Bumgarner <email@hidden>)

  • Prev by Date: Re: SCM Error 150000 ("pbxuser is not under version control")
  • Next by Date: Target 10.4 from 10.5, but getting a crash (Bus Error) right on launch
  • Previous by thread: Re: Compile error: "multiple methods named '-initWithSize:' found"
  • Next by thread: Re: Can't Compile cpp file from editor?
  • Index(es):
    • Date
    • Thread