【发布时间】:2025-11-28 21:25:02
【问题描述】:
我正在尝试创建一个获取当前时间的快速应用程序,然后创建一个在午夜结束的倒计时。这是我的第一个 swift 项目,我真的不知道发生了什么。如果有人可以帮助我,我将不胜感激,谢谢!
我得到的错误:Closure contains control flow statement cannot be used with result builder 'ViewBuilder'
代码:
//
// ContentView.swift
// RealClock
//
// Created by Milos Dragan Ivancic Santana on 3/1/22.
//
import SwiftUI
struct ContentView: View {
@State var hour = Calendar.current.component(.hour, from: Date())
@State var minute = Calendar.current.component(.minute, from: Date())
@State var second = Calendar.current.component(.second, from: Date())
@State var realHour:Int = 0
@State var realMinute:Int = 0
@State var realSecond:Int = 0
@State var isTimeShown:Bool = false
func time() {
realHour = 24-hour
realMinute = 60-minute
realSecond = 60-second
}
var body: some View {
ZStack {
VStack{
Spacer()
HStack {
while true { **I get the error here**
if isTimeShown {
Text(String(realHour))
Text(":")
Text(String(realMinute))
Text(":")
Text(String(realSecond))
}
}
}.padding(.bottom, 100)
HStack {
Button {
Timer.scheduledTimer(withTimeInterval: 0, repeats: false) {
Timer in
time()
isTimeShown = true
}
} label: {
Text("Press")
}
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
}
}
}
【问题讨论】:
-
错误很明显,您不能在
body中使用while查看TimelineView
标签: ios swift while-loop swiftui control-flow