[英]Swift workaround for protocols comparison
我知道比較協議沒有任何意義,但我的情況取決於我之前做出的選擇和決定。 表視圖的數據源是一個 RowViewModel 數組。
protocol RowViewModel {}
那里(還)沒有任何東西使它甚至符合 Equatable。 然后我的表有不同的單元格,所有這些單元格都實現了該協議:
func getCells() -> [RowViewModel] {
var rows = [RowViewModel]()
rows.append(Cell1ViewModel())
rows.append(Cell2ViewModel())
rows.append(Cell3ViewModel())
return rows
}
單元格視圖 model:
class Cell1ViewModel: RowViewModel {
var cellTitle: String
...
}
這種結構很方便,但它現在讓我望而卻步,因為我現在需要計算 delta 以發送特定的 tableView 索引來插入/刪除行。 要計算增量,我需要 RowViewModel 符合 Equatable,這是可能的,但似乎是一種解決方法,它違背了使用這種方法的初始點。 我想做這樣的事情:
let oldList = rows
let newList = getCells()
let deltaAdded = newList.filter { !oldList.contains($0) }.compactMap { newList.firstIndex(of: $0) }
let deltaRemoved = oldList.filter { !newList.contains($0) }.compactMap { oldList.firstIndex(of: $0) }
這里的最佳做法是什么? 有沒有辦法為符合 RowViewModel 的具體類型編寫比較 function?
正如我在評論中所說,你會得到類似的東西:
class CellViewModel1: Equatable {
// classes need explicit equatable conformance.
static func == (lhs: CellViewModel1, rhs: CellViewModel1) -> Bool {
// your implementation
return true
}
}
enum RowViewModel: Equatable {
// enum automatically is Equatable as long as all cases have Equatable associated types
case cell1(CellViewModel1)
}
func test() {
let oldList = [RowViewModel]()
let newList = [RowViewModel]()
let deltaAdded = newList.filter { !oldList.contains($0) }.compactMap { newList.firstIndex(of: $0) }
let deltaRemoved = oldList.filter { !newList.contains($0) }.compactMap { oldList.firstIndex(of: $0) }
}
請注意, enum 和 ViewModels 都必須符合Equatable
。 仍然不能 100% 確定這是否符合您的需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.