Ios 关联类型Swift 3
我试图理解Swift中的泛型文化,所以我写了一个小例子。但它不编译 错误:无法推断通用参数“p” 我不明白,因为我做错了Ios 关联类型Swift 3,ios,generics,swift3,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 = Som
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 {
}
协议\u 1{
关联T型
}
协议{}
struct SomeStruct_2:Protocol_A{}
struct SomeStruct_1:协议_1{
typealias T=协议A
}
设struct1=SomeStruct_1()
无法推断testFunction(t:struct1)/*泛型参数“P”*
func testFunction(t:P),其中P.t:Protocol_A{
}
testFunction中的p.T不能符合协议A,但您可以检查它是否等于协议A
func testFunction<P: Protocol_1>(t: P) where P.T == Protocol_A {
}
func testFunction(t:P),其中P.t==Protocol\u A{
}
在Swift中,因此不能将协议A
用作符合协议A
的类型。