我正在尝试制作秒表应用程序。
代码:
import SwiftUI
struct StopWatchButton : View {
var actions: [() -> Void]
var labels: [String]
var color: Color
var isPaused: Bool
var body: some View {
let buttonWidth = (UIScreen.main.bounds.size.width / 2) - 12
return Button(action: {
if self.isPaused {
self.actions[0]()
} else {
self.actions[1]()
}
}) {
if isPaused {
Text(self.labels[0])
.foregroundColor(.white)
.frame(width: buttonWidth,
height: 50)
} else {
Text(self.labels[1])
.foregroundColor(.white)
.frame(width: buttonWidth,
height: 50)
}
}
.background(self.color)
}
}
struct ContentView : View {
@ObservedObject var stopWatch = StopWatch()
var body: some View {
VStack {
Text(self.stopWatch.stopWatchTime)
.font(.custom("courier", size: 70))
.frame(width: UIScreen.main.bounds.size.width,
height: 300,
alignment: .center)
HStack{
StopWatchButton(actions: [self.stopWatch.reset, self.stopWatch.lap],
labels: ["Reset", "Lap"],
color: Color.red,
isPaused: self.stopWatch.isPaused())
StopWatchButton(actions: [self.stopWatch.start, self.stopWatch.pause],
labels: ["Start", "Pause"],
color: Color.blue,
isPaused: self.stopWatch.isPaused())
}
VStack(alignment: .leading) {
Text("Laps")
.font(.title)
.padding()
List {
ForEach(self.stopWatch.laps.identified(by: \.uuid)) { (LapItem) in
Text(LapItem.stringTime)
}
}
}
}
}
}
StopWatch.swift视图文件来自here。
我得到“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”]中的错误>
struct ContentView : View { @ObservedObject var stopWatch = StopWatch() var body: some View { VStack { Text(self.stopWatch.stopWatchTime) .font(.custom("courier", size: 70))
在“ VStack {”行中的一部分
我仅在添加了最后一个VStack部分后才收到此错误:
VStack(alignment: .leading) { Text("Laps") .font(.title) .padding() List { ForEach(self.stopWatch.laps.identified(by: \.uuid)) { (LapItem) in Text(LapItem.stringTime) } } }
[我怀疑可能是由于列表的缘故,我什至尝试在多个位置添加Group {},但它无济于事,也无法在StopWatch.swift file中找到任何修正。我是Swift和Xcode的新手。为什么会发生这种情况,我该如何解决?
我正在尝试制作秒表应用。代码:import SwiftUI struct StopWatchButton:查看{var操作:[()->无效] var标签:[String] var颜色:Color var isPaused:Bool ...
我能够通过添加id
字段来进行编译。