无法使用超过2条ForEach语句构建SwiftUI

当我尝试使用SwiftUI构建Xcode项目时,出现以下错误。

我得到的错误是“编译器无法在合理的时间对该表达式进行类型检查;尝试将表达式分解为不同的子表达式”

我试图消除代码的许多复杂性,以尽可能简化代码。我还尝试减少Picker的行数以测试是否是问题所在。

struct ContentView: View {
    // set child name
    @State private var childName: String = ""
    // set the days of the week
    let daysOfWeek = ["Monday","Tuesday","Wednesday","Thursday","Friday"]

    // set empty
    @State private var selectedAttendance: String = ""
    let attendance = ["None","All Day","AM","PM"]
    @State private var mondayAttendance = 0
    @State private var tuesdayAttendance = 0
    @State private var wednesdayAttendance = 0
    @State private var thursdayAttendance = 0
    @State private var fridayAttendance = 0

    @State private var selectedProvider = 0
    let providers = ["School 1","School 2","School 3","School 4","School 5"]

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Your Child's Details")) {
                    TextField("Name",text: $childName)
                }

                // PICKER to choose childcare provider
                Section(header: Text("Who looks after your child?")) {
                    Picker("Childcare Provider",selection: $selectedProvider) {
                        ForEach(0 ..< providers.count) {
                            Text("\(self.providers[$0])")
                        }
                    }
                }


                Section(header: Text("Which days does your child attend?")) {
                    VStack {

                        // Monday
                        HStack {
                            Text("Monday")
                                .dayOfWeek()
                            Picker("Monday",selection: $mondayAttendance) {
                                ForEach(0 ..< 4) {
                                    Text("\(self.attendance[$0])")
                                }
                            }
                            .pickerStyle(SegmentedPickerStyle())
                        }

                        // Tuesday
                        HStack {
                            Text("Tuesday")
                                .dayOfWeek()
                            Picker("Tuesday",selection: $tuesdayAttendance) {
                                ForEach(0 ..< 4) {
                                    Text("\(self.attendance[$0])")
                                }
                            }
                            .pickerStyle(SegmentedPickerStyle())
                        }
//
//                        // Wednesday
//                        HStack {
//                            Text("Wednesday")
//                                .dayOfWeek()
//                            Picker("Wednesday",selection: $wednesdayAttendance) {
//                                ForEach(0 ..< 4) {
//                                    Text("\(self.attendance[$0])")
//                                }
//                            }
//                            .pickerStyle(SegmentedPickerStyle())
//                        }


                    }


                }

            }
            .navigationBarTitle("Your Child")
        }

    }
}

当我尝试仅显示“星期一”和“星期二”进行构建时,它可以工作。一旦取消注释星期三(或更多),构建就会失败,并且会出现错误。

我知道我需要简化代码,但是它已经非常简单了,我不确定下一步该怎么做。

非常感谢您的帮助。

uxxi198549 回答:无法使用超过2条ForEach语句构建SwiftUI

For Each无关。附加到文本的dayOfWeek()的定义在哪里。它应该返回一个视图。即使有一个For Each,它也应该失败,甚至只有一个dayOfWeek()。可能是在您的应用中定义了修饰符。

本文链接:https://www.f2er.com/3127389.html

大家都在问