创建 if 语句时,我得到错误类型 () cannot confirm to View
When creating if statement, I get the error type () cannot confirm to View
在swiftui中创建if语句时,显示错误Type () cannot conform to View.
我的代码如下所示:
var body: some View {
// ************************
if cookieCounter == 1 {
cookieCounter = cookieCounter + 1
}
// ************************
ZStack {
Color("ColorBlue")
.ignoresSafeArea()
Spacer()
VStack(spacing: 20) {
// MARK: - HEADER
Spacer()
Text("Cookie Clicker Remake")
.font(.system(size: 25))
.fontWeight(.medium)
.foregroundColor(.white)
.padding()
Spacer()
Text(String(cookieCounter))
.fontWeight(.heavy)
.font(.system(size: 50))
.multilineTextAlignment(.center)
.foregroundColor(.white)
Spacer()
我是 Xcode 的新手,所以我不确定问题出在哪里。
View
的 body
是系统用来生成你的 UI 的 属性,它不是你可以 运行 你想要的代码。它也可能会比您预期的执行更多次,基本上每次系统发现影响您的视图的内容发生变化时。
如果你想增加你的计数器,你必须在发生某些事情时这样做。
您希望它在视图出现时发生吗?然后使用 .onAppear
修饰符。想在初始化器上做吗?然后为你的View
写一个init
。想通过点击按钮来完成吗?把它放在按钮的动作中。
在swiftui中创建if语句时,显示错误Type () cannot conform to View.
我的代码如下所示:
var body: some View {
// ************************
if cookieCounter == 1 {
cookieCounter = cookieCounter + 1
}
// ************************
ZStack {
Color("ColorBlue")
.ignoresSafeArea()
Spacer()
VStack(spacing: 20) {
// MARK: - HEADER
Spacer()
Text("Cookie Clicker Remake")
.font(.system(size: 25))
.fontWeight(.medium)
.foregroundColor(.white)
.padding()
Spacer()
Text(String(cookieCounter))
.fontWeight(.heavy)
.font(.system(size: 50))
.multilineTextAlignment(.center)
.foregroundColor(.white)
Spacer()
我是 Xcode 的新手,所以我不确定问题出在哪里。
View
的 body
是系统用来生成你的 UI 的 属性,它不是你可以 运行 你想要的代码。它也可能会比您预期的执行更多次,基本上每次系统发现影响您的视图的内容发生变化时。
如果你想增加你的计数器,你必须在发生某些事情时这样做。
您希望它在视图出现时发生吗?然后使用 .onAppear
修饰符。想在初始化器上做吗?然后为你的View
写一个init
。想通过点击按钮来完成吗?把它放在按钮的动作中。