在自己的init方法中调用init方法可以吗? [英] Is it okay to call an init method in self, in an init method?

查看:189
本文介绍了在自己的init方法中调用init方法可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我意识到我需要为我所拥有的帮助器类的init方法添加一个参数. helper类处理警报视图,因此它在init中已经有很多参数,这些参数将被查看,调整并发送到警报视图.

Recently I realized I needed to add an argument to the init method for a helper class I've got. The helper class deals with alert views so it already has a bunch of arguments in the init, which are looked at, tweaked, and then sent on to the alert view.

由于我在不同的地方都使用该方法,所以我不想冒崩溃的风险(错过其中一个地方,并在客户手中得到一个无法识别的选择器"),所以我决定添加第二个init方法.

Since I'm using the method as it is in various places, I don't want to risk crashing (by missing one of those places and getting an 'unrecognized selector' in the hands of a customer) so I decided to add a second init method.

- (id)initWithA:B:C:D:

- (id)initWithA:B:C:foo:D:

现在,我只是简单地将第一个实现的实现复制粘贴到foo中:一个,但理想情况下,最好将第一个调用成为第二个,即

Right now I've simply copy pasted the first one's implementation into the foo: one, but ideally what would be nice is making the first call the second, i.e.

- (id)initWithA:a B:b C:c D:d
{
    return [self initWithA:a B:b C:c foo:nil D:d];
}

但是我不确定这是否可以接受.代码似乎运行正常.

but I'm not sure if this is acceptable or not. Code appears to be working fine.

推荐答案

是的,这完全可以接受,实际上很常见.

Yes, that is perfectly acceptable and actually quite common.

这就是为什么我们有一个叫做"

This is why we have things called a "Designated Initializer". That's the initializer method to which all other initializers get redirected (usually).

这篇关于在自己的init方法中调用init方法可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆