Swift: check if generic type conforms to protocol

前端 未结 8 1519
[愿得一人]
[愿得一人] 2020-12-07 17:24

I have a protocol that I defined like so:

protocol MyProtocol {
   ...
}

I also have a generic struct:

struct MyStruct <         


        
8条回答
  •  失恋的感觉
    2020-12-07 17:49

    A modern answer will be like this: (Swift 5.1)

    func myFunc < T: MyProtocol> (s: MyStruct) -> T? {    ... }
    

提交回复
热议问题