Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a function that returns void in SwiftUI Preview's parameters

Tags:

swiftui

I'm trying to use this function:

var handler: (String, (Bool) -> Void) -> Void

And the error displayed in the preview provider is that there's a missing argument for the parameter in call, but I don't know how to do that properly.

Insert ', handler: <#(String, (Bool) -> Void) -> Void#>'

If you have some documentation or explanation about handling these types of data in the preview provider I'd be really grateful.

like image 778
carlosobedgomez Avatar asked Oct 20 '25 14:10

carlosobedgomez


1 Answers

This is a little tricky because of the closure-within-a-closure. Xcode doesn't seem to want to inline the second closure, but assuming it's defined outside of it, it seems to work fine:

struct MyView : View {
    var handler: (String, (Bool) -> Void) -> Void
    
    var body: some View {
        Text("Hello, world")
    }
}

struct TestView_Preview : PreviewProvider {
    static var boolHander : (Bool) -> Void = { _ in }
    
    static var previews: some View {
        MyView(handler:{ myString, boolHander in })
    }
}

I'm making some assumptions here, since you didn't actually include any code showing where/how it was defined, but hopefully this gets you moving in the right direction.

like image 182
jnpdx Avatar answered Oct 22 '25 04:10

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!