ios – More than one of the same if condition causes compiler type-check to fail


I was following the tutorial here:
https://youtu.be/SKkh2ZFTgdY?t=2382

At ~39:42 he finishes adding a button that is hidden until you select one of the visible buttons.

When I do that I get this error:

CompileDylibError: Failed to build DayView.swift

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

…/DayView.1.preview-thunk.swift:35:76: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
@_dynamicReplacement(for: body) private var __preview__body: some View {
^

Notice how the if selectedDate == date {... occurs twice in the code below.
If I change either one of those to if true {... then the error goes away.

Could someone explain why that would make the error go away? Am I not allowed to have two of the same if statements within this context? Is there a way around that?

I wonder if I’m not understanding something about how @State vars work and maybe I’m accidentally creating an infinite loop?

struct DayView: View {
    // dates that can be selected
    @State var dates = [
        Calendar.current.date(byAdding: .hour, value: 1, to: Date())!,
        Calendar.current.date(byAdding: .hour, value: 2, to: Date())!,
        Calendar.current.date(byAdding: .hour, value: 3, to: Date())!,
        Calendar.current.date(byAdding: .hour, value: 4, to: Date())!,
    ]
    
    // watching which date is selected
    @State var selectedDate: Date? // ? makes it optional, sometimes we don't have a selected date
    
    var body: some View {
        ScrollView {
            VStack {
                Text("Today's Date")
                Divider()
                    .padding(.vertical)
                Text("Select A Time")
                    .font(.largeTitle)
                    .bold()
                ForEach(dates, id:\.self) { date in
                    HStack {
                        Button {
                            withAnimation {
                                selectedDate = date
                            }
                        } label: {
                            Text(date.timeFromDate())
                                .bold()
                                .padding()
                                .frame(maxWidth: .infinity)
                                .foregroundColor(selectedDate == date ? .white : .blue)
                                .background(
                                    ZStack {
                                        // change one of these to "if true" and the error goes away 1/2
                                        if selectedDate == date {
                                            // the button is selected
                                            RoundedRectangle(cornerRadius: 10)
                                                .fill(.blue.opacity(0.2))
                                        }
                                        else {
                                            // the button is not selected
                                            RoundedRectangle(cornerRadius: 10)
                                                .stroke()
                                        }
                                    }
                                )
                        }
                        // change one of these to "if true" and the error goes away 2/2
                        if selectedDate == date {
                            // the button is selected, insert a button so they can navigate to another page
                            NavigationLink {
                                EmptyView()
                            } label: {
                                Text("Next")
                                    .bold()
                                    .padding()
                                    .frame(maxWidth: .infinity)
                                    .background(
                                        RoundedRectangle(cornerRadius: 10)
                                            .foregroundColor(.blue)
                                    )
                            }
                        }
                    }
                }
            }
        }
        .navigationTitle("Day Of The Week")
        .navigationBarTitleDisplayMode(.inline)
    }
}

The second occurance of if selectedDate == date {... is where that dark blue NavigationLink(basically a button) is added. That dark blue button only appears if you select one of the lighter buttons. That’s why it’s placed within the if condition.

enter image description here

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img