sergdort / CleanArchitectureRxSwift

Example of Clean Architecture of iOS app using RxSwift
MIT License
3.9k stars 496 forks source link

[Question] ViewModel protocol #4

Closed AYastrebov closed 7 years ago

AYastrebov commented 7 years ago

So, my idea is to hide ViewModel implementation with protocol

something like this: protocol PostsViewModelType: ViewModelType {}

final class PostsViewModel: PostsViewModelType {}

And then, in viewController, access the viewModel like so: var viewModel: PostsViewModelType

But I am getting the folowing error, when accessing viewModel Protocol 'PostsViewModelType' can only be used as a generic constraint because it has Self or associated type requirements

This happens because of associatedtype, but unfortunately I couldn't figure out how to resolve this. Do you have any thoughts about this?

sergdort commented 7 years ago

I wonder why do you want to hide it with protocol?

The idea is that ViewController receive concrete implementation of view model, where all other dependencies can be injected via ViewModel initializer

AYastrebov commented 7 years ago

Ok, I've got it

wikwie commented 5 years ago

I guess - for the sake of easily testing View Controller with UI / Snapshot tests.