I have a generic class, this should simply map a model into a view-model (and other stuff, but mostly this one).
class InfoProv <M, VM: CreationableFromModel> {
var models = [M]()
var viewModels = [VM]()
func generateModelView() -> VM {
return VM(model: M)
}
}
protocol CreationableFromModel {
typealias Model
init(model: Model)
}
The conformity to the protocol CreationableFromModel
tells that the view model must know how to create itself by using a Model type.
I really do not understand how to make "pass" to the VM init a valid instance of Model
There are only minor problems in your code
protocol CreationableFromModel {
typealias Model
init(model: Model)
}
// you need a generic constraint to create a connection between the two generic types
class InfoProv <M, VM: CreationableFromModel where VM.Model == M> {
var models = [M]()
var viewModels = [VM]()
func generateModelView(m: M) -> VM {
// you were passing type M here, you need an instance m of type M
return VM(model: m)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.