Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI with associated ViewModel Type can't be initialised from its hosting ViewController with Swift 5.7

I am defining a SwiftUI view with an associated ViewModel protocol type. However, I getting this error while building Type 'any MyViewModelType' cannot conform to 'MyViewModelType'.

Here’s my full code.

protocol MyViewModelType: ObservableObject {
    var loadData: CGFloat { get }
}


struct MyView<ViewModel>: View where ViewModel: MyViewModelType {
    @ObservedObject private var viewModel: ViewModel

    init(viewModel: any MyViewModelType) {
        self.viewModel = viewModel as! ViewModel
    }

    var body: some View {
        Text("Hi")
    }
}

class SubscriptionViewV2Controller: UIHostingController<MyView<MyViewModelType>> {
    init(viewModel: any MyViewModelType) {
        super.init(rootView: MyView(viewModel: viewModel))
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }

    @MainActor @objc required dynamic init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I am not sure what am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Additionally, I dont know why Xcode would throw error here

init(viewModel: any MyViewModelType) {
    self.viewModel = viewModel as! ViewModel
}

Because of that error, I need to force cast viewModel assignment.

>Solution :

SwiftUI generics need one concrete Type to work with. This could be any MyViewModelType. But you have to specify that it will be only one specific. any is the exact opposite.

protocol MyViewModelType: ObservableObject {
    var loadData: CGFloat { get }
}

struct MyView<T>: View where T: MyViewModelType {
    @ObservedObject private var viewModel: T

    init(viewModel: T) {
        self.viewModel = viewModel
    }

    var body: some View {
        Text("Hi")
    }
}

class SubscriptionViewV2Controller<T>: UIHostingController<MyView<T>> where T: MyViewModelType {
    init(viewModel: T) {
        super.init(rootView: MyView(viewModel: viewModel))
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }

    @MainActor @objc required dynamic init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

This could shed some light on it.

Also this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading