我对scrollToBottom
和UIScrollView
有一个UITableView
函数。问题是它们相互冲突,错误是:Declarations in extensions cannot override yet
这就是我所拥有的:
extension UIScrollView {
func scrollToBottom(animated: Bool = true) {
...
}
}
extension UITableView {
func scrollToBottom(animated: Bool = true) {
...
}
}
因为
UITableView
继承自UIScrollView
,所以它不允许我这样做。我怎样才能做到这一点? 最佳答案
创建协议ScrollableToBottom
并在其中定义方法:
protocol ScrollableToBottom {
func scrollToBottom(animated: Bool)
}
使
UIScrollView
和UITableView
从中继承:extension UIScrollView: ScrollableToBottom { }
extension UITableView: ScrollableToBottom { }
然后您只需要将协议约束扩展到特定的类:
extension ScrollableToBottom where Self: UIScrollView {
func scrollToBottom(animated: Bool = true) {
}
}
extension ScrollableToBottom where Self: UITableView {
func scrollToBottom(animated: Bool = true) {
}
}
关于swift - 如何重写继承类的扩展中的函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49040542/