最近需要建立UIButton的子类。
先看一看swfit中继承父类构造方法的条件:
Rule1 1
“If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.”
也就是说,如果你不在子类中实现任何designated 构造函数,就可以继承父类的所有designated 和 convience 构造函数。
再看一个复杂点的例子,这个例子是swfit的官方说明文档上的:
其中需要注意的就是第二个框中的内容
“The init(name: String) convenience initializer provided by RecipeIngredient takes the same parameters as the init(name: String) designated initializer from Food. Because this convenience initializer overrides a designated initializer from its superclass, it must be marked with the override modifier (as described in Initializer Inheritance and Overriding). Even though RecipeIngredient provides the init(name: String) initializer as a convenience initializer, RecipeIngredient has nonetheless provided an implementation of all of its superclass’s designated initializers. Therefore, RecipeIngredient automatically inherits all of its superclass’s convenience initializers too.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l
这里可以看出,子类可以建立convinience 的构造方法去override 父类的designated 构造方法,也算实现了父类的designated 构造方法。
下面看具体的实验过程
1.先建立这个空的类,编译,无任何问题。
class TestBtn: UIButton { }
2.继续添加
class TestBtn: UIButton { init(){ } }
这次编译后,有以下截图
为什么我不写 init() 方法就不需要实现 init(coder aDecoder: NSCoder) 方法呢,这里就设计到了开始提到的构造函数继承问题,由于我们的子类没有designated 构造方法,那么我们所写的这一个init 方法就被系统当做了designated 构造方法,所以我们的类不能继承UIButton的任何构造方法,而nscoding协议又要求UIButton实现这个构造方法,所以出现了警告。
3.我们消除警告后,代码如下,继续编译
class TestBtn: UIButton { init(){ } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
错误如下图
这个比较好理解,没有调用直接父类的designated 构造函数。
4. 更改代码如下,继续编译
class TestBtn: UIButton { init(){ super.init() } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
错误如下
就是说UIButton的init方法并不是一个 designated 构造方法。
UIButton无自定义init方法,因此是继承与UIControl的,而UIControl也无自定义init,因此是UIView的,而UIView的只有一个
init(frame: CGRect),那么它一定是一个designated 构造函数!
正确的写法如下:
class TestBtn: UIButton { init(){ super.init(frame: CGRectZero) } required init(coder aDecoder: NSCoder) { super.init(coder:aDecoder) } }