Displaying an empty view in SwiftUI

前端 未结 3 2206
情话喂你
情话喂你 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

    You have to return something. If there is some condition where you want to display nothing, "display" an...EmptyView ;)

    var body: some View {
        Group {
            if text != nil {
                Text(text!)
            } else {
                EmptyView()
            }
        }
    }
    

    The SwiftUI DSL will require you to wrap the if/else in a Group and the DSL has no guard/if let nomenclature.

提交回复
热议问题