前言
多继承和多重代理在swift的语言层面上是不支持的,但我们有时会遇到这样的问题:
类B和C分别继承自A,B1和B2继承自B,C1和C2继承自C.现在我们需要在B1和C1中添加相同的方法,怎么去做?使用继承的话只能在类A中添加,但这样做的结果是基类A会越来越臃肿,最后变成上帝类God Class,维护起来会很困难. 在实现完某个代理后发现,我们还要在其他页面中获取数据.例如,IM消息接收之后要在多个地方做回调,比如显示消息内容页面,改变小红点,显示消息数.即一对多的模式,我们第一反应是用通知,但通知还是能少用就少用,用多了代码的可阅读性会大大降低.面对第一种情况,最好的解决方法是,B1和C1的公共方法专门封装到一个地方,需要的时候就调用一下,多继承就是一个最好的解决方案.
1. 多继承
1. 实现过程
swift中的类可以遵守多个协议,但是只可以继承一个类,而值类型(结构体和枚举)只能遵守单个或多个协议,不能做继承操作.
多继承的实现:协议的方法可以在该协议的extension中实现
protocol Behavior {
func run()
}
extension Behavior {
func run() {
print("Running...")
}
}
struct Dog: Behavior {}
let myDog = Dog()
myDog.run() // Running...
无论是结构体还是类还是枚举都可以遵守多个协议,所以要实现多继承,无非就是多遵守几个协议的问题.
下面举个例子.
2. 通过多继承为UIView扩展方法
// MARK: - 闪烁功能
protocol Blinkable {
func blink()
}
extension Blinkable where Self: UIView {
func blink() {
alpha = 1
UIView.animate(
withDuration: 0.5,
delay: 0.25,
options: [.repeat, .autoreverse],
animations: {
self.alpha = 0
})
}
}
// MARK: - 放大和缩小
protocol Scalable {
func scale()
}
extension Scalable where Self: UIView {
func scale() {
transform = .identity
UIView.animate(
withDuration: 0.5,
delay: 0.25,
options: [.repeat, .autoreverse],
animations: {
self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
})
}
}
// MARK: - 添加圆角
protocol CornersRoundable {
func roundCorners()
}
extension CornersRoundable where Self: UIView {
func roundCorners() {
layer.cornerRadius = bounds.width * 0.1
layer.masksToBounds = true
}
}
extension UIView: Scalable, Blinkable, CornersRoundable {}
cyanView.blink()
cyanView.scale()
cyanView.roundCorners()

这样,如果我们自定义了其他View,只需要放大和缩小效果,遵守Scalable协议就可以啦!
3. 多继承钻石问题(Diamond Problem),及解决办法
请看下面代码
protocol ProtocolA {
func method()
}
extension ProtocolA {
func method() {
print("Method from ProtocolA")
}
}
protocol ProtocolB {
func method()
}
extension ProtocolB {
func method() {
print("Method from ProtocolB")
}
}
class MyClass: ProtocolA, ProtocolB {}
此时ProtocolA和ProtocolB都有一个默认的实现方法method(),由于编译器不知道继承过来的method()方法是哪个,就会报错.

