In SwiftUI there's frequently a need to display an "empty" view based on some condition, e.g.:
struct OptionalText: View {
let text: String?
var body: some View {
guard let text = text else { return }
return Text(text)
}
}
Unfortunately, this doesn't compile since the body of guard
has to return some view, that is an "empty" view when text
is nil
. How should this example be rewritten so that it compiles and renders an "empty" view when text
is nil
?