I have a simple SwiftUI View like the following.
struct TestView: View {
var callBack: ((String) -> Void)
var body: some View {
Button {
callBack("George")
} label: {
Text("Done!")
.frame(width: 72)
}
}
}
#Preview("Test") {
let callBack: (String) -> Void = { _ in }
TestView(callBack: callBack)
}
So I'm returning a String variable with a closure. If I have two variables to return with a closure, I only make the callBack guy optional in order to avoid having an error (Constant 'callBack' used before being initialized) in preview.
struct TestView: View {
var callBack: ((String, Int) -> Void)?
var body: some View {
Button {
callBack?("George", 48)
} label: {
Text("Done!")
.frame(width: 72)
}
}
}
#Preview("TestView") {
TestView()
}
I wonder if I can return more than one variable without having an error in Preview? Thanks a lot.