简体   繁体   English

如何解决 SwiftUI 中的“编译器无法进行类型检查……”错误?

[英]How can I solve “The compiler is unable to type-check …” error in SwiftUI?

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我正在 SwiftUI 中制作老虎机游戏(出于学习语言的目的),我正在努力做到这一点,以便当您说 2 个苹果时,您会像普通老虎机游戏一样获得 50 分,但不断出现错误

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions . 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!任何帮助将不胜感激!

It is this bit of code that causes the problem:正是这段代码导致了问题:

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
}

Inside a VStack you can only have Views, not code that handles any logic.在 VStack 中,您只能拥有视图,而不是处理任何逻辑的代码。 Also what comes after if is not valid Swift code.如果不是有效的 Swift 代码,那么接下来会发生什么。 && and || &&|| can be used on Bool s not on String s.可以在Bool上使用,而不是在String上使用。

Your logic for if and it place is wrong, look at right logic and place!您对if和 it place 的逻辑是错误的,请查看正确的逻辑和 place! You cannot run logic code in place that wants View!您不能在需要查看的地方运行逻辑代码! Also all your 3 if doing 1 thing! if做 1 件事,你的 3 也一样! you do not need use the name of "apple", "cherry", "star" the meaning of code is important.你不需要使用“apple”、“cherry”、“star”的名称,代码的意义很重要。

Also you do not need use this condition (Slot1 == Slot2 && Slot1 == Slot3) because of this condition Slot1 == Slot2 or this condition Slot1 == Slot3 .也不需要使用这个条件(Slot1 == Slot2 && Slot1 == Slot3)因为这个条件Slot1 == Slot2或这个条件Slot1 == Slot3 Once one them became true , then there is no need for (Slot1 == Slot2 && Slot1 == Slot3) , otherwise you would gave some extra credits as bonus like { credits += 250 } , then you should have another if for that!一旦他们成为true ,那么就不需要(Slot1 == Slot2 && Slot1 == Slot3) ,否则你会像{ credits += 250 }这样给予一些额外的学分作为奖励,那么你应该有另一个if


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()
                
                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)]
                    Slot2 = pictures[Int.random(in: pictures.indices)]
                    Slot3 = pictures[Int.random(in: pictures.indices)]
                    
                    
                    
                    
                    if (Slot1 == Slot2) || (Slot2 == Slot3) || (Slot1 == Slot3) {     //<<: Here!
                        
                        if (Slot1 == Slot2 && Slot1 == Slot3) {                       //<<: Here!
                            
                            credits += 250
                            
                        }
                        else {
                            
                            credits += 50
                        }
                        
                    }
                    
                    
                    
                    
                }, label: {
                    
                    ZStack {
                        RoundedRectangle(cornerRadius: 25)
                            .frame(width: 200, height: 50)
                        
                        Text("Spin")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .foregroundColor(Color.white)
                    }
                    
                })
                
                Spacer()
                
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查 (Xcode 12.0 SwiftUI) - Xcode Compiler error: The compiler is unable to type-check this expression in reasonable time (Xcode 12.0 SwiftUI) 编译器无法在合理的时间内对该表达式进行类型检查 SWIFTUI? - The compiler is unable to type-check this expression in reasonable time SWIFTUI? 编译器无法在 SwiftUI 中的合理时间内对该表达式进行类型检查? - The compiler is unable to type-check this expression in a reasonable time in SwiftUI? SwiftUI:! [Array] 编译器无法在合理的时间内对该表达式进行类型检查 - SwiftUI:! [Array] the compiler is unable to type-check this expression in reasonable time SwiftUI 代码太长,编译器无法对其进行类型检查,但我似乎无法将其分解为多个部分 - SwiftUI code too long for the compiler to type-check it, but I can't seem to break it down into parts 当我遍历列表中的数组时,出现“编译器无法进行类型检查”错误? - When I loop through an array in a list, I get "compiler is unable to type-check" error? 编译器无法对 Swift 5 表达式进行类型检查? - The compiler is unable to type-check expression Swift 5? 编译器无法对该表达式 swift 4 进行类型检查? - The compiler is unable to type-check this expression swift 4? 声明数组时出现编译器错误:编译器无法进行类型检查……不同的子表达式 - Compiler error while declaring an array: compiler is unable to type-check … distinct sub-expressions 编译器无法在合理时间内对该表达式进行类型检查 --&gt; Xcode swift - The compiler is unable to type-check this expression in reasonable time --> Xcode swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM