私と同じ問題を提示しているこの投稿を認識していますが、未回答で古いため、ここで更新すると思いました。
//: Playground - noun: a place where people can play
import UIKit
protocol BaseViewModel { }
protocol SomeCellViewModelInterface : BaseViewModel { }
protocol AnotherCellViewModelInterface : BaseViewModel { }
protocol BaseCell {
typealias T
func configure(viewmodel: T)
}
//
class someCell : UIView, BaseCell {
typealias T = SomeCellViewModelInterface
func configure(viewmodel: T) {
// awesome
}
}
class someOtherCell : UIView, BaseCell {
typealias T = AnotherCellViewModelInterface
func configure(viewmodel: T) {
// do stuff
}
}
// the concrete implementations of viewmodels and actually using this UI is ultimatley in another .framework
class ConcreteSomeCellVM : SomeCellViewModelInterface { }
class ConcreteAnotherCellVM : AnotherCellViewModelInterface { }
var viewModel = ConcreteSomeCellVM()
let views: [UIView] = [someCell(), someOtherCell(), UIView()]
これは私が必要とするものの初歩的な例ですが、要点を示しています
for v in views {
// A
if let cell = v as? someCell {
cell.configure(viewModel)
}
// B
if let cell = v as? BaseCell {
cell.configure(viewModel)
}
}
ブロック A は機能しますが、具体的なセル クラスを知る必要があるため、多くの異なるセルのリストがある場合、その具体的な型がわからないものは機能しません。
ブロック B は次のエラーをスローします。
エラー: プロトコル 'BaseCell' は、Self または関連型の要件があるため、一般的な制約としてのみ使用できます
ブロック B を達成する方法はありますか?