![](/img/trans.png)
[英]How to solve the error "Redundant conformance of viewController to protocol UIScrollViewDelegate in swift?
[英]Error: Redundant conformance of 'ViewController' to protocol with extension
当我尝试这个我得到错误:
class ViewController: UIViewController, UIScrollViewDelegate {
......
}
extension ViewController: UIScrollViewDelegate { // Error: Redundant conformance of 'ViewController' to protocol 'UI
....
}
当我尝试这个我没有错误:
class ViewController: UIViewController {
......
}
extension ViewController: UIScrollViewDelegate { // No error
...
}
使用扩展程序时,为什么不将UIScrollViewDelegate添加到ViewController?
如果类是UIViewController的类型,则表示它符合UIScrollViewDelegate吗?
在第一个代码示例中,您已经通过类声明为UIScrollViewDelegate
添加了一致性。
现在,当您尝试使用扩展名再次符合UIScrollViewDelegate
时,迅速对您尖叫。
对于第二个代码示例,在扩展名中添加了一致性。 添加扩展名之前,该类不符合UIScrollViewDelegate
。
该错误是不言自明的。 您不必多次遵守协议。 您可以做1)
class ViewController: UIViewController, UIScrollViewDelegate {
......
}
要么
2)
class ViewController: UIViewController {
......
}
extension ViewController: UIScrollViewDelegate { // No error
...
}
在情况1中,您不需要扩展,因为类本身采用了协议。 扩展的目的是向类添加更多功能。 在情况2中,很明显该扩展采用了该协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.