Better to ask with example. So its table sections and rows
First created a protocol which needs to be implemented by all rows which has an associated type, must be given by caller to tell while defining row.
protocol RowElementProtocol {
associatedtype ElementType
var cellIdentifier: String {get set}
var cellType: ElementType {get set}
}
So creating a generic row struct here
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
}
}
Creating a different row struct here
struct DifferentRow<T>: RowElementProtocol {
var cellIdentifier = "cell"
var cellType: T
/// Some Different Properties other then generic
}
Now creating a section which can have any kind of rows
struct Section<T: RowElementProtocol> {
var rows = 1
var rowElements: [T]
}
Everything is fine here, Problem arises when I want to initialise an array of sections
let sections = [Section<T: RowElementProtocol>]()
Compiler is not allowing me to initialise. Its showing ">' is not a postfix unary operator".