加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift - Designated Initializer & Convenience Initial

发布时间:2020-12-14 07:08:59 所属栏目:百科 来源:网络整理
导读: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 des

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出现

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读