问题
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)?
回答1:
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.
来源:https://stackoverflow.com/questions/29563701/new-in-xcode-6-3-ios-8-3-using-self-alloc-for-convenience-constructor-causes-bu