Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 关联类型未隐式定义的泛型协议_Ios_Swift_Generics_Protocols_Associated Types - Fatal编程技术网

Ios 关联类型未隐式定义的泛型协议

Ios 关联类型未隐式定义的泛型协议,ios,swift,generics,protocols,associated-types,Ios,Swift,Generics,Protocols,Associated Types,我试图隐式定义关联的类型,但出现错误: “RowProtocol”对于此上下文中的类型查找不明确 然后,您可以使用以下方法对其进行初始化: let implicitRow = Row(cellClass: Cell.self) 如何实现这一点?符合Row协议要求将关联的类型T映射到具体类型,而Row则不能做到这一点。我假设您还希望使行通用,这就是为什么您没有从协议中为t指定类型别名的原因 解决方案是将行设置为通用: struct Row<T>: RowProtocol {

我试图隐式定义关联的类型,但出现错误:

“RowProtocol”对于此上下文中的类型查找不明确

然后,您可以使用以下方法对其进行初始化:

let implicitRow = Row(cellClass: Cell.self)

如何实现这一点?

符合
Row协议要求将关联的类型
T
映射到具体类型,而
Row
则不能做到这一点。我假设您还希望使
通用,这就是为什么您没有从协议中为
t
指定类型别名的原因

解决方案是将
设置为通用:

struct Row<T>: RowProtocol {
    let cellClass: T.Type
    init(cellClass: T.Type) {
        self.cellClass = cellClass
    }
}

您需要将
T
映射到某个对象
struct Row<T>: RowProtocol {
    let cellClass: T.Type
    init(cellClass: T.Type) {
        self.cellClass = cellClass
    }
}
// exactly the same struct, but with different name for the generic argument.
struct Row<U>: RowProtocol {
    let cellClass: U.Type
    init(cellClass: U.Type) {
        self.cellClass = cellClass
    }
}