本文介绍了Xcode 6.3/iOS 8.3中的新增功能:使用self alloc进行便捷构造函数会导致生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

//...

如果我按Cmd +单击initWithType:调用,则会在CAEmitterBehavior中看到冲突,这是我们项目中完全没有引用的对象,但是我猜想它在iOS 8.3中必须是新的.

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.

如果将[self alloc]更改为[AGNetworkRequest alloc],则继承此方法的子类将仅返回父对象,该父对象的行为与我们设计此类的方式相反.

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)?

推荐答案

显示您的分配返回.

[(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).

应该返回实例类型而不是id来解决此问题(allocWithZone将自动返回实例类型...),但是有可能是因为您使用的是'self'静态信息不足.

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.

这篇关于Xcode 6.3/iOS 8.3中的新增功能:使用self alloc进行便捷构造函数会导致生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:26