无法编译SwiftUI ForEach表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法编译SwiftUI ForEach表相关的知识,希望对你有一定的参考价值。

我正在尝试从动态数组制作表视图,但是编译器无法使用不相关的错误来构建它:编译器无法在合理的时间内对这个表达式进行类型检查;尝试将表达式分解为不同的子表达式

struct RecentView: View {
    @State var recentRooms: [[String: String]] = []

    @Binding var recentIsPressed: Bool
    @Binding var nextIsPressed: Bool
    @Binding var conferenceName: String

    let screenWidth = UIScreen.main.bounds.size.width

    var body: some View {
        if let recent = UserDefaults.standard.array(forKey: "recentRooms") as? [[String: String]] {
            recentRooms = recent
        }

        return VStack {

            ...

            VStack(alignment: .leading) {
                ForEach(0 ..< recentRooms.count) { i in
                    Button(action: { self.meetingSelected(name: self.recentRooms[i]["name"] ?? "") }) {
                        HStack {
                            ZStack {
                                Circle()
                                    .foregroundColor(Color("ProjectBlue"))
                                    .frame(width: 76, height: 76)
                                Text(self.recentRooms[i]["short_name"] ?? "")
                                    .font(.custom("Roboto-Bold", size: 36))
                                    .foregroundColor(Color("AlmostWhite"))
                            }
                            VStack(alignment: .leading) {
                                Text(self.recentRooms[i]["name"] ?? "")
                                    .font(.custom("OpenSans-Bold", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                                Text(self.recentRooms[i]["date"] ?? "")
                                    .font(.custom("OpenSans-Regular", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                                Text(self.recentRooms[i]["duration"] ?? "")
                                    .font(.custom("OpenSans-Regular", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                            }
                        }
                    }
                    .frame(width: self.screenWidth, alignment: .leading)
                    .padding(.leading, 20)
                }
            }
        }
    }

    private func meetingSelected(name: String) {
        conferenceName = name
        nextIsPressed = true
        recentIsPressed = false
    }
}

关于ForEach部分有什么问题的任何建议?

答案

ForEach的此变体不应与动态内容一起使用。

ForEach(0 ..< recentRooms.count)

如果可以更改recentRooms变量,请改用此变量:

ForEach(recentRooms, id: .self)

以上是关于无法编译SwiftUI ForEach表的主要内容,如果未能解决你的问题,请参考以下文章