OGeek|极客世界-中国程序员成长平台

标题: ios - 关联类型 Swift 3 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:54
标题: ios - 关联类型 Swift 3

我正在尝试理解 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 '' could not be inferred*

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

}



Best Answer-推荐答案


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

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

关于ios - 关联类型 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40783044/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4