代码之家  ›  专栏  ›  技术社区  ›  abbood

如何使Swift中的一个类扩展为一个具体的类并符合多个协议[[副本]

  •  0
  • abbood  · 技术社区  · 7 年前

    背景

    我正在为创建一个扩展 UITableViewController

    func attachToController<C: UIViewController>(_ sender: C, alignTo: UIView, to: ALEdge = .top , withOffset: CGFloat = 0.0)
        where C: UITableViewDataSource, C:UITableViewDelegate
    {
        self.estimatedRowHeight = 44
        ..
        self.delegate = sender as UITableViewDelegate
        self.dataSource = sender as UITableViewDataSource
    }
    

    final class MyViewController: UIViewController {
    

    然后,我将其称为view Dod load:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupTableView()
    }
    
    ..
    
    func setupTableView() {
        self.tableView.attachToController(self, alignTo: self.view, withOffset: 0.0)
        ..
     }
    

    但我得到了错误

    我想申报 MyViewController 这样它就扩展了 UIViewController UITableViewDataSource UITableViewDelegate 协议。。思想?

    注意:我使用的是Swift 4.2。

    1 回复  |  直到 7 年前
        1
  •  1
  •   olejnjak    7 年前

    再加上

    final class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
    

    如果 attachToController(...) 分机接通了 UITableViewController UITableView