The Error is thrown since your Coordinator apparently expects the myImage not to be Binding<Optional<UIImage>>
but rather the Binding<UIImage>
. The difference is significant, and the solution will depend on the desired design of your code.
1. Get rid of Optionals
Remove the ?
to no longer deal with Optional, this will probably not suite your need but it's an option.
// in your parent
@State private var myImage: UIImage
// and child implementation
@Binding var myImage: UIImage
2. Introduce Optionals to the Coordinator
Add the ?
to make the Binding<UIImage>
a Binding<Optional<UIImage>>
. Again, may not be best solution for your needs.
// in your Coordinator implementation
@Binding var myImage: UIImage?
3. Provide new Binding with some default value
I would recommend the following:
func makeCoordinator() -> Coordinator {
Coordinator(parent: self, myImage: Binding(
get { myImage ?? UIImage() }, // or some other default value
set { myImage = $0 }
))
}
The solution really depends on what you are trying to achieve really. You need to decide if you actually want the Optional to be in the Coordinator or not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…