【问题标题】:"Unable to infer complex closure return type; add explicit type to disambiguate" Error in SwiftUI“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”SwiftUI 中的错误
【发布时间】:2020-07-06 18:19:17
【问题描述】:

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

代码:

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 还很陌生。 为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: ios swift xcode swiftui stopwatch


    【解决方案1】:

    我可以通过添加 id 字段来编译它。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多