Swift generic type conforming to two protocols

混江龙づ霸主 提交于 2019-12-05 20:07:05

问题


I have a generic method in one of my classes where I want to have a generic type conforming to UIViewController and UIPickerViewDelegate. How can I do that? I thought of doing this:

func foo<T: UIViewController, UIPickerViewDelegate> (#viewController: T) {}

But this code doesn't "recognize" the UIPickerViewDelegate. I also thought of using the pipe | instead of the comma but this is even worse, the compiler doesn't accept that. Is it possible to do this or do I have to do 2 parameters for the class and the protocol? Or is there a nicer workaround?

Thanks for your help and Merry Christmas :]


回答1:


Your code:

func foo<T: UIViewController, UIPickerViewDelegate> (#viewController: T) {}

declares 2 generics parameters:

  • T which is UIViewController. And it's used as viewController parameter type.
  • UIPickerViewDelegate which is Any. And it's not used.

Instead, you should use "Where Clause", like:

func foo<T: UIViewController where T:UIPickerViewDelegate> (#viewController: T) {}



回答2:


Things have been changed in Swift 4: func foo<T: UIViewController> (viewController: T) where T:UIPickerViewDelegate {}




回答3:


Since Swift 4 you can use the power of Protocol Composition. Here you go:

func foo<T: UIViewController & UIPickerViewDelegate> (viewController: T) {}


来源:https://stackoverflow.com/questions/27633872/swift-generic-type-conforming-to-two-protocols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!