Associatedtype Swift 3

时间:2016-11-24 09:53:47

标签: ios generics swift3

我正在尝试在 Swift 中理解泛型文化,所以我写了一个小例子。但它没有编译。

错误:无法推断通用参数“P” 我无法理解我做错了。

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 {

}

1 个答案:

答案 0 :(得分:2)

testFunction中的P.T不能符合Protocol_A,但你可以检查它是否等于Protocol_A。

func testFunction<P: Protocol_1>(t: P) where P.T == Protocol_A {
}
相关问题