Starting with Swift 4:
Thanks to SE-0156 :
&, AnyObject, . Objective-C :
AnyObject & Protocol1 & Protocol2
Base & Protocol
Objective-C, , Protocol1 Protocol2, Base, Protocol.
.. :
var v1 : UIViewController & P1
Swift 4:
. v1 UIViewController, P1, casting (as), .
, , . :
func testGeneric<T: UIViewController where T: P1>(input: T) {
input.f1()
}
, , , , :
protocol Delegate: class {
func doDelegate()
}
class Controller {
func doController() {
print("Controller")
}
}
class ConcreteController: Controller, Delegate {
func doDelegate() {
print("Delegate")
}
}
class View {
private weak var controller: Controller? = nil
private weak var delegate: Delegate? = nil
func setDelegateController<T: Controller where T: Delegate>(delegateController: T?) {
controller = delegateController
delegate = delegateController
}
func test() {
controller?.doController()
delegate?.doDelegate()
}
}
:
let view = View()
let controller = ConcreteController()
view.setDelegateController(controller)
view.test()