我对scrollToBottomUIScrollView有一个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)
}

使UIScrollViewUITableView从中继承:
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/

10-12 07:16