0

today I'm wondering if it's possible to work designing a view on SwiftUI using PreviewProvider It's difficult when you have models because you need to "Initialize" every Model and Attribute to see the UI Emulator. Ex

    struct SimpleView: View {
    
    @State private var number : Int 
    
    
    var body: some View {
        ScrollView{
            VStack{
                Text(String(number))
            }
        }
    }
}


struct SimpleView_Previews: PreviewProvider {
    static var previews: some View {
        SimpleView(Int: 5)
    }
}

UI Preview Works good!

But what if I need to use a custom object with 40-70 properties... :(

Is there a way to work without "hardcode" in the beginning?

Thank you so much

4
  • 2
    I normally either create a MockViewModel subclass of the relevant ViewModel or if the view model is a struct I create a static function on te struct that returns an appropriate "mock" instance of the struct. Commented Dec 10, 2020 at 1:00
  • 1
    with 40-70 properties - don't do that, ever - separate on small parts, the smaller is the better. Commented Dec 10, 2020 at 4:08
  • I'll try to separate that models because it's annoying to load an entire model (unfortunately there are so many models that works together! I mean, an Address Form with 20 attributes like zip code, email, name, telephone... It's a mess) Commented Dec 10, 2020 at 5:55
  • @Paulw11 could you show me an example please? Commented Dec 10, 2020 at 5:56

1 Answer 1

1

One approach is to create a mockViewModel class property that returns an instance with fixed values that you can use for previews.

In this example, the backing model object is a Core Data managed object.

In normal use you call the ProjectDetailViewModel(project:) initialiser.

For the preview you can use the mockViewModel property

class ProjectDetailViewModel: ObservableObject {
    
    @Published var name: String {
        didSet {
            self.project.name = name
        }
    }
    
    @Published var notes: String {
        didSet {
            self.project.notes = notes
        }
    }
    
    let project: Project!
    
    init(project: Project) {
        self.project = project
        name = project.name ?? ""
        notes = project.notes ?? ""
    }
    
    private init() {
        project = nil
        name = "Mock project"
        threePhase = false
        notes = "Mock Notes"
    }
    
    static var mockViewModel:ProjectDetailViewModel {
        return ProjectDetailViewModel()
    }
    
}


struct ProjectDetailView_Previews: PreviewProvider {
    static var previews: some View {
        ProjectDetailView(project: ProjectDetailViewModel.mockViewModel)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.