У меня есть простой протокол для любого вида расширяемых моделей:
protocol PSDiffableModelProtocol: Hashable {
var identifier: UUID { get }
}
extension PSDiffableModelProtocol {
func hash(into hasher: inout Hasher) { hasher.combine(identifier) }
static func == (lhs: Self, rhs: Self) -> Bool { lhs.identifier == rhs.identifier }
}
Ну и собственно структура, реализующая этот протокол:
struct PSModel1: PSDiffableModelProtocol, Decodable {
let identifier = UUID()
let title: String
let subtitle: String
let additionalSubtitle: String
let imageUrl: URL
let footnote: String?
}
struct PSModel2: PSDiffableModelProtocol, Decodable { /* ... */ }
// ....
Каждая модель должна храниться в другой распространяемой моделимодель:
struct PSSection<Model: PSDiffableModelProtocol>: PSDiffableModelProtocol {
let identifier = UUID()
let title: String?
let subtitle: String?
let models: [Model]
}
И тогда возникает простой вопрос, но как мне хранить мои разделы в одном месте? Вот так:
var sections: [PSSection] = [
PSSection(..., models: [PSModel1(...), PSModel1(...), ]),
PSSection(..., models: [PSModel2(...), PSModel2(...), ]),
// ...
]