Displaying an empty view in SwiftUI

前端 未结 3 2282
情话喂你
情话喂你 2021-02-20 18:02

In SwiftUI there\'s frequently a need to display an \"empty\" view based on some condition, e.g.:

struct OptionalText:          


        
3条回答
  •  生来不讨喜
    2021-02-20 18:45

    As of Xcode 12 beta 2 the Group view is no longer needed and if let declarations are supported, so the resulting body can be a bit more succint:

    var body: some View {
        if let text = text {
            Text(text)
        } else {
            EmptyView()
        }
    }
    

提交回复
热议问题