Class conforming to protocol as function parameter in Swift

前端 未结 7 1269
萌比男神i
萌比男神i 2020-11-29 02:12

In Objective-C, it\'s possible to specify a class conforming to a protocol as a method parameter. For example, I could have a method that only allows a UIViewControlle

7条回答
  •  借酒劲吻你
    2020-11-29 02:51

    With Swift 3, you can do the following:

    func foo(_ dataSource: UITableViewDataSource) {
        self.tableView.dataSource = dataSource
    }
    
    func foo(_ delegateAndDataSource: UITableViewDelegate & UITableViewDataSource) { 
        //Whatever
    }
    

提交回复
热议问题