Re: Incompatible pointer type assigning to Warning
Re: Incompatible pointer type assigning to Warning
- Subject: Re: Incompatible pointer type assigning to Warning
- From: Clark Cox <email@hidden>
- Date: Wed, 25 May 2016 07:09:35 -0700
> On May 25, 2016, at 06:33, Dave <email@hidden> wrote:
>
>
> Hi,
>
> I’m getting a warning on the following:
>
> #import “LTWClassX.h"
>
>
> LTWGadgetX* myGadgetX;
> Class myClass;
>
> myClass = NSClassFromString(theClassName);
> if (myClass == nil)
> return nil;
>
> myGadgetX = [myClass newGadgetInfo]; //*****Warning - incompatible pointer type assigning to LTWGadgetX from LTWGadgetZ
>
>
> newGadgetInfo is define as so:
>
> +(LTWGadgetX*) newGadgetInfo;
>
> In LTWGadgetX.h and LTWGadgetX.m
>
> This code used to use LTWGadgetZ and didn’t produce the warning, now I’ve changed it, it still remembers the LTWGadgetZ definition, which still exists but isn’t included.
>
> In LTWGadgetZ.h and LTWGadgetZ.m
>
> +(LTWGadgetZ*) newGadgetInfo;
>
> I’ve tried cleaning the project but it still moans, how do I fix this?
The compiler doesn’t know which method you’re actually calling, and has no way of disambiguating at compile time, so it has to assume that it is one or the other; in this case, it’s assuming that it’s LTWGadgetZ. (i.e. it’s not a matter of it “remembering” old code, the selection is more or less arbitrary). In this case, You have several options:
- Cast (i.e. myGadgetX = (LTWGadgetX *)[myClass newGadgetInfo])
- Change +newGadgetInfo to return id
- Use “id” for myGadgetX’s type
- Use a common superclass of LTWGadgetX and LTWGadgetZ for myGadgetX’s type
- Make a protocol to which LTWGadgetX and LTWGadgetZ both conform, that declares the methods they have in common, and use “id<ThatProtocol>” for myGadgetX’s type
--
Clark Smith Cox III
email@hidden
_______________________________________________
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