编译器无法在合理的时间内在SwiftUI中对该表达式进行类型检查

时间:2019-10-17 13:42:56

标签: swift swiftui

我试图在SwiftUI中构建我认为简单的表单,但是却收到错误消息:“编译器无法键入检查此表达式;请尝试从主体返回的视图上将表达式分成不同的子部分(在本例中为Form,但我也尝试过VStack)。如果我开始删除某些子视图,即使我的视图都没有一个直接返回10个子视图,这似乎也可以解决,我认为这是单个视图的限制(ForEach中返回的视图除外)。

import SwiftUI

struct CreateDispatchView: View {
    @State private var customerId: Int = 0
    @State private var name: String = ""
    @State private var phone: String = ""
    @State private var description: String = ""
    @State private var priorityId: Int = 0
    @State private var callTypeId: Int = 0
    @State private var bay: String = ""
    @State private var poTicket: String = ""
    @State private var outOfChemicals: Bool = false
    @State private var isReturnCall: Bool = false

    var body: some View {
        Form { // <-- Error is shown here
            Section {
                Picker("Customer", selection: $customerId) {
                    ForEach(0 ..< 10) {
                        Text("Customer \($0)")
                    }
                }
                TextField("Name", text: $name)
                TextField("Phone", text: $phone)
                TextField("Description", text: $description)
                TextField("Bay", text: $bay)
                TextField("PO ticket", text: $poTicket)
            }

            Section {

                Picker("Priority", selection: $priorityId) {
                    ForEach(0 ..< 2) {
                        Text("Priority \($0)")
                    }
                }
                Picker("Call type", selection: $callTypeId) {
                    ForEach(0 ..< 3) {
                        Text("Call type \($0)")
                    }
                }
            }

            Section {

                Toggle(isOn: $outOfChemicals) {
                    Text("Out of chemicals")
                }
                Toggle(isOn: $isReturnCall) {
                    Text("Is return call")
                }
            }
        }
    }
}

struct CreateDispatchView_Previews: PreviewProvider {
    static var previews: some View {
        CreateDispatchView()
    }
}




1 个答案:

答案 0 :(得分:2)

处理大型嵌套ViewBuilder表达式是当前swift编译器的弱点。一般来说,尽管如此,它的“尝试将表达式分成不同的子部分”的建议是一个很好的建议:将这些整体式ViewBuilder表达式重构为单独的动态变量是提高性能的好方法(此外,它有助于找出实际错误)。

这是一个示例,说明如何重构代码以使其成功编译:

struct CreateDispatchView: View {
    @State private var customerId: Int = 0
    @State private var name: String = ""
    @State private var phone: String = ""
    @State private var description: String = ""
    @State private var priorityId: Int = 0
    @State private var callTypeId: Int = 0
    @State private var bay: String = ""
    @State private var poTicket: String = ""
    @State private var outOfChemicals: Bool = false
    @State private var isReturnCall: Bool = false

    var body: some View {
        Form {
            customerSection
            prioritySection
            infoSection
        }
    }

    private var customerSection: some View {
        Section {
            Picker("Customer", selection: $customerId) {
                ForEach(0 ..< 10) {
                    Text("Customer \($0)")
                }
            }
            TextField("Name", text: $name)
            TextField("Phone", text: $phone)
            TextField("Description", text: $description)
            TextField("Bay", text: $bay)
            TextField("PO ticket", text: $poTicket)
        }
    }

    private var prioritySection: some View {
        Section {
            Picker("Priority", selection: $priorityId) {
                ForEach(0 ..< 2) {
                    Text("Priority \($0)")
                }
            }
            Picker("Call type", selection: $callTypeId) {
                ForEach(0 ..< 3) {
                    Text("Call type \($0)")
                }
            }
        }
    }

    private var infoSection: some View {
        Section {
            Toggle(isOn: $outOfChemicals) {
                Text("Out of chemicals")
            }
            Toggle(isOn: $isReturnCall) {
                Text("Is return call")
            }
        }
    }
}