Type-check error in Xcode 12: "The compiler is unable to type-check this expression in reasonable time"

Viewed 189

I have updated my Xcode to version 12 few days ago. When I opened one of my existing projects the new Xcode is presenting the error:

"The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions"

The project was working fine in the previous version.

Here is my code where I get the error:

    import SwiftUI

struct FaqView: View {
    @ObservedObject var viewModel = FaqViewModel()
    @State var sectionState: [String: Bool] = [:]
    
    init(){
        UITableView.appearance().backgroundColor = .init(red: 255, green: 255, blue: 255, alpha: 0.0)
        UITableViewCell.appearance().backgroundColor = .init(red: 255, green: 255, blue: 255, alpha: 0.0)
    }
    var body: some View {
        GeometryReader { geometry in
             return VStack(spacing: 0) {
                    VStack {
                        CustomHeader(titleText: "FAQ LIST") {
                            DashboardView()
                        }
                    }
                    .frame(width: geometry.size.width)
                    .padding(.top, 10)
                    .background(Color("B1"))
                    ZStack {
                        Image("App Background")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .offset(y: -geometry.size.height/2.8)
                            .padding(.top, 100)
                        VStack {
                            List {
                                if self.viewModel.faqModel?.payload?.count ?? 0 > 0 {
                                    ForEach(self.viewModel.faqModel?.payload?[0] ?? []) { payload in
                                        Text("\(payload.title ?? "")".uppercased())
                                            .foregroundColor(Color("T7"))
                                            .padding(.leading, 22)
                                        ForEach(payload.child ?? []) { section in
                                            Section(
                                                header: HStack {
                                                    Image(self.sectionState[section.title ?? ""] ?? false ? "FAQ Minus Icon" : "FAQ Plus Icon")
                                                        .resizable()
                                                        .frame(width: self.sectionState[section.title ?? ""] ?? false ? 16 : 15, height: self.sectionState[section.title ?? ""] ?? false ? 3 : 15)
                                                    Text(section.title ?? "")
                                                        .foregroundColor(self.sectionState[section.title ?? ""] ?? false ? Color("T2") : Color("T8"))
                                                        .font(.system(size: 16))
                                                        .padding(.top, -2)
                                                }
                                                .onTapGesture {
                                                    withAnimation {
                                                        self.sectionState[section.title ?? ""] = !self.isExpanded(section.title ?? "")
                                                    }
                                                }
                                            ) {
                                                if self.isExpanded(section.title ?? "") {
                                                    VStack {
                                                        Text("\(section.childDescription ?? "")")
                                                            .padding(.leading, 22)
                                                            .foregroundColor(Color("T8"))
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            .listStyle(GroupedListStyle())
                        }
                        if self.viewModel.isFAQListAppeared {
                            LoaderView()
                                .offset(y: -70.0)
                        }
                    }
                    Spacer()
                }
                .edgesIgnoringSafeArea(.all)
                .onAppear {
                    self.viewModel.isFAQListAppeared = true
                    self.viewModel.fetchWithAF()
                }
            }
            
        }
        func isExpanded(_ section: String) -> Bool {
            sectionState[section] ?? false
        }
}

struct FaqView_Previews: PreviewProvider {
    static var previews: some View {
        FaqView()
    }
}

If anyone has experienced with this error please help. Thanks in advance.

1 Answers

I'm not completely sure but I think that the way how you are unwrapping the optional arrays in the ForEach's is wrong. Unfortunately I can't verify it because I don't know the FaqViewModel but if I comment everything else except the ForEach line I get the error "Cannot convert value of type '[Any]' to expected argument type 'Range'"

Related