Skip navigation.
 
mlRe: warning: assignment from distinct Objective-C type
FROM : Stuart Malin
DATE : Tue Mar 11 19:03:10 2008

Brilliant Julien!  Thank you!

I had always been curious why initializers returned id, since their 
return could be made specific to the class.

I've been doing what I've done in many classes, and some give me 
grief, and not others.

Both of your assessments are correct.

To validate #1, if I change the offending line of code to do a cast:

   xmppStream = [(XMPPStream*)[XMPPStream alloc] initWithDelegate:self];

the warning goes away. Of course, that's ugly and I won't retain that 
code change because the deeper problem is exactly what you refer to 
in item #2 -- I do have more than one class with the same initializer 
signature. I'd thought the compiler would choose the right one 
because I'd naively presumed that [className alloc]  returned an 
instance in that class, not an object of type id.

This is  probably why I had to cast in my initializer:

   - (XMPPStream*) initWithDelegate:(id)initialDelegate
   {
   self = (XMPPStream*)[super init];

If I change my initializer method name as you suggest  so that it has 
a unique signature,

   - (XMPPStream*) initXMPPStreamWithDelegate:(id)initialDelegate;

and the creation of an instance accordingly:

   xmppStream = [[XMPPStream alloc] initXMPPStreamWithDelegate:self];

all now compiles cleanly.

Now, the question is: is it bad to continue doing what I've been 
doing in having the initializer interface defined to not return id 
but an instance of the actual class?  Or would my continuing to do 
this merely be a non-conventional coding style?

--Stuart

On Mar 10, 2008, at 11:16 PM, Julien Jalon wrote:

> 1) All init methods should return (id) not a specific class
> 2) your initWithDelegate: is likely too generic as a name and its 
> signature conflicts with an other one.
>
> Since [XMPPStream alloc] is typed id, the compiler might not be 
> sure of what method signature you want to use for -initWithDelegate:
>
> In the second case, the compiler knows you are using -[XMPPStream 
> initWithDelegate:]
>
> I tend to remember that the compiler warning was more specific 
> before Leopard, maybe a regression.
>
> Nonetheless, good coding style should lead you to rename your 
> method to:
>
> - (id)initXMPPStreamWithDelegate:(id)aDelegate;
>
> instead of:
>
> - (XMPPStream *)initWithDelegate:(id)aDelegate;
>
> --
> Julien
>
> On Tue, Mar 11, 2008 at 9:19 AM, Stuart Malin 
> <<email_removed>> wrote:
> I have a line of code:
>
>        xmppStream = [[XMPPStream alloc] initWithDelegate:self];
>
> That when compiled, receives a warning:
>
> warning: assignment from distinct Objective-C type
>
> Now, what's odd to me, is if I change the source code to this:
>
>        xmppStream = [XMPPStream alloc];
>        [xmppStream initWithDelegate:self];
>
> It compiles without any warning.
>
> The interface for the XMPPStream initializer is:
>
>        - (XMPPStream*) initWithDelegate:(id)initialDelegate;
>
> And, in the file that is allocating and initializing, the xmppStream
> variable is defined as:
>
>        XMPPStream      *xmppStream;
>
> So I don't see any reason that I should get the warning.
> Any clues about why this happens would be appreciated.
> I'm sure I must be overlooking something obvious...
>
> This is happening with Xcode 2.4.1
>
> [I have posted to both Cocoa and Xcode because I have no idea which
> list would be better for this]
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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/<email_removed>
>
> This email sent to <email_removed>
>

Related mailsAuthorDate
mlwarning: assignment from distinct Objective-C type Stuart Malin Mar 11, 09:19
mlRe: warning: assignment from distinct Objective-C type Ron Fleckner Mar 11, 09:42
mlRe: warning: assignment from distinct Objective-C type Stuart Malin Mar 11, 09:50
mlRe: warning: assignment from distinct Objective-C type Julien Jalon Mar 11, 10:16
mlRe: warning: assignment from distinct Objective-C type Keary Suska Mar 11, 16:57
mlRe: warning: assignment from distinct Objective-C type Stuart Malin Mar 11, 19:03
mlRe: warning: assignment from distinct Objective-C type Julien Jalon Mar 11, 20:48
mlRe: warning: assignment from distinct Objective-C type Chris Hanson Mar 11, 21:42
mlRe: warning: assignment from distinct Objective-C type Stuart Malin Mar 11, 22:09
mlRe: warning: assignment from distinct Objective-C type Mike Abdullah Mar 11, 22:58
mlRe: warning: assignment from distinct Objective-C type Stuart Malin Mar 11, 23:49
mlRe: warning: assignment from distinct Objective-C type Quincey Morris Mar 11, 23:49
mlRe: warning: assignment from distinct Objective-C type j o a r Mar 11, 23:59
mlRe: warning: assignment from distinct Objective-C type mmalc crawford Mar 12, 01:56
mlRe: warning: assignment from distinct Objective-C type Quincey Morris Mar 12, 02:11
mlRe: warning: assignment from distinct Objective-C type mmalc crawford Mar 12, 06:39
mlRe: warning: assignment from distinct Objective-C type Dennis C. De Mars Mar 12, 17:26
mlRe: warning: assignment from distinct Objective-C type Keith Duncan Mar 12, 23:33