快速继承协议方法覆盖 [英] swift inherited protocol method override

查看:36
本文介绍了快速继承协议方法覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这会奏效:

protocol Foo : UITableViewDelegate{
}

extension Foo{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("ROW:\(indexPath.row)")
    }
}

class MyVC : UITableViewController, Foo{
   //...
}

不幸的是,该方法不会被调用.我错过了什么,或者这真的不可能吗?

No luck though, the method does not get called. Am I missing something, or is this genuinely not possible?

此外,没有编译器警告/错误,只是没有调用该方法,有些人可能会认为这是一个错误.

Also, there is no compiler warning/error, the method is just not being called, which some may consider a bug.

推荐答案

我认为这是因为您是从 UITableViewController 继承的,它已经实现了 UITableViewDelegate.

I think this is because your are inheriting from UITableViewController which already implements UITableViewDelegate.

您可以轻松测试:

class MyVC: UITableViewController {
  func tableView(tableView tv: UITableView, 
                 didSelectRowAtIndexPath ip: NSIndexPath) {}
}

这将导致

error: overriding declaration requires an 'override' keyword

编译器假定该方法已经在直接继承链中实现.因此,不会选择协议中的默认方法.

The compiler assumes the method is already implemented in the direct inheritance chain. Hence your default method from the protocol won't be picked.

话虽如此,下面的变体也不起作用.将 @objc 添加到协议使我的 swiftc 段错误...:

Having said that, the variant below does not work either. Adding @objc to the protocol makes my swiftc segfault ...:

import UIKit

protocol Foo : UITableViewDelegate {
  func tableView(tableView: UITableView, didSelectRowAtIndexPath: NSIndexPath)
}

extension Foo {

  func tableView(tableView: UITableView,
                 didSelectRowAtIndexPath _: NSIndexPath)
  {
    print("abcXXX")
  }

}

class ViewController: UIViewController, UITableViewDataSource, Foo {

  override func loadView() {
    self.view = UITableView(frame: CGRect())
  }

  var tableView : UITableView { return self.view as! UITableView }

  override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate   = self
    self.tableView.dataSource = self
    self.tableView.registerClass(UITableViewCell.self,
                                 forCellReuseIdentifier: "MyCell1")
  }

  func tableView(tableView: UITableView, numberOfRowsInSection _: Int) -> Int {
    return 3
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath ip: NSIndexPath)
        -> UITableViewCell
  {
    let cell = tableView
                 .dequeueReusableCellWithIdentifier("MyCell1", forIndexPath: ip)
    cell.textLabel?.text = "p[\(ip.row)]"
    return cell
  }
}

因此我假设我的答案只有 50% 是正确的.大概协议扩展只适用于静态绑定上下文,不包括 ObjC 协议?

Hence I assume my answer is only 50% right. Presumably protocol extensions only work in static binding contexts, which excludes ObjC protocols?

更新:这似乎是正确的答案:使用 UIKit 进行面向协议的编程.本质:

Update: This seems to be the proper answer: Protocol-Oriented Programming with UIKit. The essence:

我们不能做什么:为 Objective-C 协议提供默认实现.

What we CAN'T do: Provide default implementations for Objective-C protocols.

这篇关于快速继承协议方法覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆