swift - Designated Initializer & Convenience Initializers

前端之家收集整理的这篇文章主要介绍了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相关文章