Xcode 6.3/iOS 8.3中的新功能:使用self alloc方便构造函数会导致构建错误

ray*_*ray 5 objective-c subclassing ios xcode6.3

此代码在Xcode 6.2和6.3之间没有变化,但[self alloc]现在包含的行会导致错误:

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
}

//...
Run Code Online (Sandbox Code Playgroud)

如果我Cmd +点击了这个initWithType:电话,我会看到冲突CAEmitterBehavior,我们项目中没有引用的对象,但我猜测必须是iOS 8.3中的新功能.

如果我更改了[self alloc]to [AGNetworkRequest alloc],继承此方法的子类将只返回父对象,这与我们设计此类的方式相反.

如何在不更改方法名称的情况下消除冲突(这需要更改整个应用程序中的所有方法调用)?

Kir*_*ani 5

转换你的分配回报.

[(AGNetworkDataRequest*)[self alloc] initWithType:type];
Run Code Online (Sandbox Code Playgroud)

这将为编译器提供足够的信息来进行调用.如果编译器不知道参数的长度,则调用有可能在运行时失败(并且可能非常难以调试).

返回instancetype而不是id应该解决这个问题(allocWithZone将自动返回instancetype ...),但这是可能的,因为你使用'self'没有足够的静态信息.