I try to unwrap my optional property, but get this Error message:
Closure containing control flow statement cannot be used with function builder 'ViewBuilder'
I don't see whats wrong with my code
HStack {
if let height = profile.height {
TagBox(field: "height", value: String(height))
}
TagBox(field: "nationality", value: profile.nationality)
Spacer()
}.padding(.horizontal)
Using conditional binding in a ViewBuilder
is perfectly fine now:
HStack {
if let height = profile.height { // <- This works now in Xcode 12
TagBox(field: "height", value: String(height))
}
TagBox(field: "nationality", value: profile.nationality)
Spacer()
}.padding(.horizontal)
There are two ways to work with optionals in this context:
The first one, if you don't want this view to show at all if profile.height is nil:
profile.height.map({ TagBox(field: "height", value: String($0))})
The second one, if you want this view to show, but with a default value instead:
TagBox(field: "height", value: String(profile.height ?? 0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With