Reputation: 1428
I would like to pass an evaluating function as a parameter to a custom component but it is not working. I am not sure if I should create a custom closure. Would love to hear your thoughts. Here is the code:
@State private var selectedBeautyFilter: BeautyLUT
...
CheckmarkView(isChecked: (selectedView == .none) ? true : false)
PS I am able to get the code working by just using a if..else statement but I insert CheckmarkView component twice (one for isChecked true and another one false)
Cheers
Upvotes: 1
Views: 329
Reputation: 1
You can use any function for print("Hello 1")
or print("Hello 2")
it is just for showing, for example like this: customFunction = yourCustomFunctionName
in this example I send a custom function to a View. Even you can do same thing to send a function to another function no big issue, all possible.
import SwiftUI
struct ContentView: View {
@State private var customFunction: (() -> Void)?
var body: some View {
Spacer()
Button("send func 1") {
customFunction = { print("Hello 1") }
}
.padding()
Button("send func 2") {
customFunction = { print("Hello 2") }
}
.padding()
Spacer()
CustomView(customFunction: $customFunction)
Spacer()
}
}
struct CustomView: View {
@Binding var customFunction: (() -> Void)?
var body: some View {
Button("run incoming func") {
if let unwrappedFuc = customFunction { unwrappedFuc() } else { print("No function send!")}
}
.padding()
}
}
Upvotes: 2