If you want to have a property type of a dynamic protocol, typealias .
protocol MyViewModel { var title: String { get set } } protocol MyViewController { typealias MyViewModelType var viewModel: MyViewModelType { get set } } class BaseViewController<T: MyViewModel>: MyViewController { typealias MyViewModelType = T var viewModel: T init(_ viewModel: T) { self.viewModel = viewModel } } struct CarViewModel: MyViewModel { var title: String = "Car" } struct BikeViewModel: MyViewModel { var title: String = "Bike" } let car = BaseViewController(CarViewModel()) let bike = BaseViewController(BikeViewModel())
If you try to use it with a model that does not match your MyViewModel protocol, this will not work:
struct AnotherModel { var title: String = "Another" } let another = BaseViewController(AnotherModel())
This has some gotchas since you cannot pass your view controller with an argument of type MyViewController because of typealias . This will not work:
func something(vc: MyViewController) { }
Why not simplify the approach without typealias . If I understand you correctly, you do not need them. Sort of:
protocol MyViewModel { var title: String { get set } } protocol MyViewController { var viewModel: MyViewModel { get set } } class BaseViewController: MyViewController { var viewModel: MyViewModel init(_ viewModel: MyViewModel) { self.viewModel = viewModel } } struct CarViewModel: MyViewModel { var title: String = "Car" } struct BikeViewModel: MyViewModel { var title: String = "Bike" }
And now you can use the MyViewController protocol as a variable type:
let bike: MyViewController = BaseViewController(BikeViewModel()) let car: MyViewController = BaseViewController(CarViewModel())
You can pass it to some function as MyViewController :
func something(vc: MyViewController) { }
And you cannot do this:
struct AnotherViewModel { var title: String = "Another" } let another: MyViewController = BaseViewController(AnotherViewModel())
Did I miss something? I mean, about your generics and type restrictions? Do you have any precedent that forces you to use them?
zrzka source share