Alternative to switch statement in SwiftUI ViewBuilder block?

前端 未结 6 1566
耶瑟儿~
耶瑟儿~ 2020-12-02 12:21

⚠️ 23 June 2020 Edit: From Xcode 12, both switch and if let statements will be supported in the ViewBuilder!

I’ve been trying to replicate an app of

6条回答
  •  借酒劲吻你
    2020-12-02 12:43

    You can do with a wrapper View

    struct MakeView: View {
        let make: () -> AnyView
    
        var body: some View {
            make()
        }
    }
    
    struct UseMakeView: View {
        let animal: Animal = .cat
    
        var body: some View {
            MakeView {
                switch self.animal {
                case .cat:
                    return Text("cat").erase()
                case .dog:
                    return Text("dog").erase()
                case .mouse:
                    return Text("mouse").erase()
                }
            }
        }
    }
    

提交回复
热议问题