Swift: check if generic type conforms to protocol

前端 未结 8 1567
[愿得一人]
[愿得一人] 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:50

    I have to say @Alex want to check if T type conforms to protocol rather than s. And some answerer didn't see clearly.

    Check T type conforms to protocol like this :

    if let _ = T.self as? MyProtocol.Type {
        //  T conform MyProtocol
    }
    

    or

    if T.self is MyProtocol.Type {
        //  T conform MyProtocol
    }
    

提交回复
热议问题