Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new in Xcode 6.3/iOS 8.3: using self alloc for convenience constructor causes build error

This code did not change between Xcode 6.2 and 6.3, but the line containing [self alloc] now causes the error:

Multiple methods named 'initWithType:' found with mismatched result, parameter type or attributes

@implementation AGNetworkDataRequest

+ (instancetype)networkDataRequestWithType:(AGNetworkDataRequestType)type
{
    AGNetworkDataRequest *r = [[self alloc] initWithType:type];//error here
    return r;
}

- (id)initWithType:(AGNetworkDataRequestType)type
{
    //typical init code
}

//...

If I Cmd+click on the initWithType: call, I am shown the conflict in CAEmitterBehavior, an object not referenced in our project at all, but I'm guessing must be new in iOS 8.3.

If I change the [self alloc] to [AGNetworkRequest alloc], the subclasses inheriting this method will just return the parent object, which acts in opposition to how we designed this class.

Any way to eliminate the conflict without changing the method name (which requires changing all method calls throughout the app)?

like image 450
ray Avatar asked Apr 10 '15 14:04

ray


1 Answers

cast your alloc return.

[(AGNetworkDataRequest*)[self alloc] initWithType:type];

This will give the compiler enough information to make the call. If the compiler doesn't know the length of your parameter there is a chance the call will fail at runtime (and potentially be very difficult to debug).

returning instancetype rather than id is supposed to fix this (allocWithZone will automatically return instancetype...) but it's possible because you're using 'self' there is not enough static information.

like image 112
KirkSpaziani Avatar answered Oct 29 '22 15:10

KirkSpaziani