有没有办法在Swift协议中覆盖可选方法?
为什么在UIKit中有类似的例子?
protocol Protocol {
func requiredMethod()
}
extension Protocol {
func optionalMethod() {
// do stuff
}
}
class A: Protocol {
func requiredMethod() {
print("implementation in A class")
}
}
class B: A {
func optionalMethod() { // <-- Why `override` statement is not required?
print("AAA")
}
}
为什么在UIKit中有类似的例子?
protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
// ......
optional public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
}
class MyTVC: UITableViewController {
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{}
override
语句是必需的!但是UITableViewController
没有响应选择器"tableView: estimatedHeightForRowAtIndexPath:"
问题出在哪里?