[英]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.