SwiftUI中的“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”

时间:2020-03-26 06:08:03

标签: ios swift xcode swiftui stopwatch

我正在尝试制作秒表应用程序。

代码:

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 {”行

Code Error

添加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的新手。 为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:1)

我可以通过添加一个id字段来对其进行编译。

identified(by:)已贬值,因此您现在应该使用init(_:id:)source

ForEach(self.stopWatch.laps, id: \.uuid) { (LapItem) in
    Text(LapItem.stringTime)
}