Nikita Ermolenko Nikita Ermolenko - 11 days ago 5
iOS Question

Associatedtype Swift 3

I'm trying understand generics culture in Swift so I've written a small example. But it does not compile.

Error: Generic parameter 'P' could not be inferred
I can't understand for I'm doing wrong.

protocol Protocol_1 {
associatedtype T
}

protocol Protocol_A {}
struct SomeStruct_2: Protocol_A {}

struct SomeStruct_1: Protocol_1 {
typealias T = Protocol_A
}


let struct1 = SomeStruct_1()
testFunction(t: struct1) // *Generic parameter 'P' could not be inferred*

func testFunction<P: Protocol_1>(t: P) where P.T : Protocol_A {

}

Answer

P.T in testFunction cannot conform to Protocol_A, but you can check if its equal to Protocol_A.

func testFunction<P: Protocol_1>(t: P) where P.T == Protocol_A {
}
Comments