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 – PreviewProvider pass nil for optional Void function

struct Toolbar: View {
    
    let closeThisView: () -> Void?
    var toolbarTitle: String
    
    var body: some View {
        
        Text("bla")
    }
}


struct Toolbar_Previews: PreviewProvider {
    
    static var previews: some View {
        Toolbar(closeThisView: nil, toolbarTitle: "comments")
            .preferredColorScheme(.dark)
    }
}

How can I pass nil there or any function since I don’t need it in the preview anyway? Like this is what I’ve also tried:

struct Toolbar_Previews: PreviewProvider {
    
    @Environment(\.dismiss) var dismiss
    func closeThisView(){

        DispatchQueue.main.async {
            self.dismiss()
        }
    }
    
    static var previews: some View {
        Toolbar(closeThisView: closeThisView, toolbarTitle: "comments")
            .preferredColorScheme(.dark)
    }
}

>Solution :

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

You can use this { }
For example:

struct Toolbar_Previews: PreviewProvider {
    
   static var previews: some View {
        Toolbar(closeThisView: {}, toolbarTitle: "comments")
            .preferredColorScheme(.dark)
    }
}
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