swift - Designated Initializer & Convenience Initializers

Designated initializers tend to set all of the properties up and let the user send in values for each. A convenience initializer often has some of those hard coded,and thus can take less parameters

To simplify the relationships between designated and convenience initializers,Swift applies the following three rules for delegation calls between initializers:

Rule 1
A designated initializer must call a designated initializer from its immediate superclass.

Rule 2
A convenience initializer must call another initializer from the same class.

Rule 3
A convenience initializer must ultimately call a designated initializer.

A simple way to remember this is:

Designated initializers must always delegate up.
Convenience initializers must always delegate across.

下面举例说明以上几点:

首先我们创建一个子类继承UIView,添加自己的一个init方法(Designated initializers):

init(frame: CGRect,bgColor: UIColor,tag: Int,superView: UIView) {

}

在这里,我们需要遵循Rule 1,调用父类的Designated initializers

init(frame: CGRect,superView: UIView) {

 super.init(frame:frame) // not super.init() self.backgroundColor = bgColor self.tag = tag superView.addSubview(self) }

接下来,我们再创建两个Convenience initializers

convenience init(bgColor:UIColor) {
   self.init(frame:CGRectZero,bgColor:bgColor)//Rule 2
}



convenience init(frame: CGRect,bgColor:UIColor) {
   self.init(frame: frame,bgColor:bgColor,tag: 0)//Rule 3
}

使用Designated Initializer的好处:

修改代码之前,应该想清楚是否需要删除原有代码。类似这种扩展,旧的接口可以留下来,然后在其中调用多参数的Designated Initializer。保证良好的编写Designated Initializer的风格,可以让我们节约很多时间,避免潜藏的bug出现

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...