Swift: check if generic type conforms to protocol

Solution 1:

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
}

Solution 2:

A bit late but you can test if something responds to protocol with as ? test:

if let currentVC = myViewController as? MyCustomProtocol {
    // currentVC responds to the MyCustomProtocol protocol =]
}

EDIT: a bit shorter:

if let _ = self as? MyProtocol {
    // match
}

And using a guard:

guard let _ = self as? MyProtocol else {
    // doesn't match
    return
}

Solution 3:

The simplest answer is: don’t do that. Use overloading and constraints instead, and determine everything up-front at compile-time rather than testing stuff dynamically at runtime. Runtime type checking and compile-time generics are like steak and ice-cream – both are nice but mixing them is a bit weird.

Consider something like this:

protocol MyProtocol { }

struct MyStruct <T>  { let val: T }

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

func myFunc<T>(s: MyStruct<T>) -> T? {
    return nil
}

struct S1: MyProtocol { }
struct S2 { }

let m1 = MyStruct(val: S1())
let m2 = MyStruct(val: S2())

myFunc(m1) // returns an instance of S1
myFunc(m2) // returns nil, because S2 doesn't implement MyProtocol

The downside being, you can’t establish dynamically if T supports a protocol at runtime:

let o: Any = S1()
let m3 = MyStruct(val: o)
myFunc(m3)  // will return nil even though o 
            // does actually implement MyProtocol

But, in all honesty, do you really need to do that inside your generic function? If you’re unsure what actual type something is, the better option may be to figure that out up-front rather than deferring it to later and prodding it inside a generic function to find out.

Solution 4:

let conforms = T.self is MyProtocol.Type

Solution 5:

you can also leverage swift's switch case pattern matching, if you want to handle multiple cases of type T:

func myFunc<T>(s: MyStruct<T>) -> T? {
    switch s {
    case let sType as MyProtocol:
        // do MyProtocol specific stuff here, using sType
    default:
        //this does not conform to MyProtocol
    ...
    }
}