EN VI

Fixing preview on SwiftUI?

2024-03-16 05:00:09
How to Fixing preview on SwiftUI

Passing data in preview with a viewModel is easy but how do i pass data when i have some kind of generic data? Can't find a proper answer on google. In this particular app i'm implementing the Router design pattern can be found here (https://github.com/obvios/Routing). The preview is asking me to pass an argument for parameter ''router'' but this is quite different like when you do with a viewModel normally i'd pass viewModel: ExampleViewModel(). So i don't know what exactly to pass in the Preview? Autocomplete is not helpful.

enter image description here

enter image description here

import SwiftUI
import Routing

 struct ContentView: View {
    var body: some View {
        RoutingView(BasicsRouter.self) { router in
            BasicsView(router: router)
        }
    }
 }


 struct BasicsView: View {
    @StateObject var router: Router<BasicsRouter>
    init(router: Router<BasicsRouter>) {
        _router = StateObject(wrappedValue: router)
    }
    
    var body: some View {
        VStack {
            Text("Hello")
        }
    }
 }

 #Preview {
    BasicsView()
 }

    

Solution:

You might need to pass ContentView in preview:

#Preview {
    ContentView()
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login