【问题标题】:Associatedtype Swift 3关联类型 Swift 3
【发布时间】:2016-11-24 09:53:47
【问题描述】:

我正在尝试理解 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 {

}

【问题讨论】:

标签: ios generics swift3


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多