最好举个例子。所以它的表格部分和行
首先创建了一个协议(protocol),该协议(protocol)需要由具有关联类型的所有行实现,必须由调用者在定义行时告知。
protocol RowElementProtocol {
associatedtype ElementType
var cellIdentifier: String {get set}
var cellType: ElementType {get set}
}
所以在这里创建一个通用的行结构
struct GenericRow <T>: RowElementProtocol {
var cellIdentifier = "cell"
var cellType: T
/// Some Other Properties
init(cellIdentifier: String = "cell", cellType: T) {
self.cellIdentifier = cellIdentifier
self.cellType = cellType
}
}
在这里创建一个不同的行结构
struct DifferentRow<T>: RowElementProtocol {
var cellIdentifier = "cell"
var cellType: T
/// Some Different Properties other then generic
}
现在创建一个可以有任何类型行的部分
struct Section<T: RowElementProtocol> {
var rows = 1
var rowElements: [T]
}
这里一切都很好,当我想初始化一个部分数组时出现问题
let sections = [Section<T: RowElementProtocol>]()
编译器不允许我初始化。它显示“>'不是后缀一元运算符”。
最佳答案
让我们看看编译器允许您创建这样一个数组的结果。
你可以这样做:
var sections = [Section<T: RowElementProtocol>]()
var sec1 = Section<GenericRow<String>>()
var sec2 = Section<DifferentRow<Int>>()
sections.append(sec1)
sections.append(sec2)
let foo = sections[0] // how can the compiler know what type this is?
现在看到问题了吗? “嗯,foo
可以只是 Section<T: RowElementProtocol>
类型”你可能会说。好吧,让我们假设这是真的:
foo.rowElements[0].cellType // what the heck is the type of this?
编译器不知道。它只知道 foo.rowElements[0]
是“某种符合 RowElementProtocol
的类型,但 cellType
可以是任何类型。
“好吧,为什么不直接使用 Any
作为那个类型呢?”你可能会问。编译器可以这样做,但这会使您的泛型变得毫无意义,不是吗?
如果您想让让您的泛型变得毫无意义,您可以通过创建类型 AnyRowElement
来执行所谓的“类型删除”。和 AnySection
:
struct AnyRowElement : RowElementProtocol {
var cellIdentifier: String
var cellType: Any
typealias ElementType = Any
init<T: RowElementProtocol>(_ x: T) {
self.cellIdentifier = x.cellIdentifier
self.cellType = x.cellType
}
}
struct AnySection {
var rows: Int
var rowElements: [AnyRowElement]
init<T: RowElementProtocol>(_ x: Section<T>) {
self.rows = x.rows
self.rowElements = x.rowElements.map(AnyRowElement.init)
}
}
let sections: [AnySection] = [AnySection(sec1), AnySection(sec2)]
关于ios - 如何快速创建通用关联类型协议(protocol)数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54376356/