I am being unable to return a View that uses a protocol as a dependency as this is throwing me Reference to generic type 'LoginView' requires arguments in <...>
.
func makeLoginView(viewModel: LoginViewModelType) -> LoginView {
return LoginView(viewModel: viewModel)
}
My LoginView uses LoginViewModelType as I have two different view models.
protocol LoginViewModelType: ObservableObject {
var bookingPaymentViewModel: BookingPaymentViewModel? { get }
var email: String { get set }
var password: String { get set }\
...
func login()
}
struct LoginView<ViewModel: LoginViewModelType>: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
...
}
I can't understand what I am doing wrong, the LoginView should be able to return a View regardless as it complies to LoginViewModelType.
CodePudding user response:
You need to constrain makeLoginView
to accept a generic of type LoginViewModelType
and then use that same generic in the returned value.
class BookingPaymentViewModel { }
func makeLoginView<T:LoginViewModelType>(viewModel: T) -> LoginView<T> {
return LoginView(viewModel: viewModel)
}
protocol LoginViewModelType: ObservableObject {
var bookingPaymentViewModel: BookingPaymentViewModel? { get }
var email: String { get set }
var password: String { get set }
func login()
}
struct LoginView<ViewModel: LoginViewModelType>: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
Text("test")
}
}