为什么在SwiftUI中两次调用外部结构

时间:2020-04-24 13:10:46

标签: swiftui

我很惊讶为什么两次调用我的结构!有人可以解释这个问题吗?无需绕过这个问题,只是好奇为什么会发生?

struct PerformanceTest: View {

    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Next Page")) {
                ButtonView()
                myPrint("test----15------") //called once
            }
        }
    }
}


struct ButtonView: View {

    var body: some View {
        VStack {
            myPrint("test----16------") //why is it called two times!
            Text("Oxygen")
                .font(.title)
                .foregroundColor(Color.red)
        }
    }
}

extension View {
    func myPrint(_ vars: Any...) -> some View {
        for v in vars { print(v) }
        return EmptyView()
    }
}

0 个答案:

没有答案