jeudi 1 avril 2021

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

I am making a slots game in SwiftUI (for the purposes of learning the language) and I am trying to make it so that when you get say 2 apples then you get 50 points like a regular slots game but keep on getting the error The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. I tried multiple methods of simplifying my code but couldn't find any method that would solve this error.

My Code

import SwiftUI

struct ContentView: View {
    
    @State private var credits = 0
    @State private var Slot1 = "apple"
    @State private var Slot2 = "cherry"
    @State private var Slot3 = "star"
    @State private var pictures = ["apple", "cherry", "star"]
    
    
    var body: some View {
        ZStack {
            
            VStack {
                
                Text("Slots using SwiftUI")
                    .font(.largeTitle)
                    .padding(.top, 50.0)
                
                Spacer()
                
                if (Slot1 && Slot2) || (Slot2 && Slot3) || (Slot1 && Slot3) || (Slot1 && Slot2 && Slot3) == "apple" {
                    credits = credits + 50
                }
                
                if (Slot1 && Slot2) || (Slot2 && Slot3) || (Slot1 && Slot3) || (Slot1 && Slot2 && Slot3) == "cherry" {
                    credits = credits + 50
                }
                
                if (Slot1 && Slot2) || (Slot2 && Slot3) || (Slot1 && Slot3) || (Slot1 && Slot2 && Slot3) == "star" {
                    credits = credits + 50
                }
            
                
                
                Text("Credits: " + credits.description)
                    .font(.title2)
                
                Spacer()
                
                HStack {
                    
                    Image(Slot1).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot2).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot3).resizable().aspectRatio(contentMode: .fit)
                }
                
                Spacer()
                
                Button(action: {
                    
                    Slot1 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    Slot2 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    Slot3 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    
                }, label: {
                    
                    ZStack {
                        RoundedRectangle(cornerRadius: 25)
                            .frame(width: 200, height: 50)
                        
                        Text("Spin")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .foregroundColor(Color.white)
                    }
                    
                })
                
                Spacer()
                
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire