Ios 关联类型未隐式定义的泛型协议
我试图隐式定义关联的类型,但出现错误: “RowProtocol”对于此上下文中的类型查找不明确 然后,您可以使用以下方法对其进行初始化: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 {
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
}
}