本文介绍了"无法推断复杂的闭包返回类型;添加显式类型以消除歧义"SwiftUI 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

代码:

导入 SwiftUI结构停止手表按钮:查看{var 动作:[() ->空白]var 标签:[字符串]var 颜色:颜色var isPaused: Boolvar主体:一些视图{让 buttonWidth = (UIScreen.main.bounds.size.width/2) - 12返回按钮(动作:{如果 self.isPaused {self.actions[0]()} 别的 {self.actions[1]()}}) {如果是暂停{文本(self.labels[0]).foregroundColor(.white).frame(宽度:按钮宽度,高度:50)} 别的 {文本(self.labels[1]).foregroundColor(.white).frame(宽度:按钮宽度,高度:50)}}.background(self.color)}}结构内容视图:查看{@ObservedObject var stopWatch = StopWatch()var主体:一些视图{虚拟堆栈{文字(self.stopWatch.stopWatchTime).font(.custom("courier", size: 70)).frame(width: UIScreen.main.bounds.size.width,高度:300,对齐:.center)堆栈{StopWatchButton(动作:[self.stopWatch.reset,self.stopWatch.lap],标签:[重置",圈"],颜色: Color.red,isPaused: self.stopWatch.isPaused())StopWatchButton(动作:[self.stopWatch.start,self.stopWatch.pause],标签:[开始",暂停"],颜色:Color.blue,isPaused: self.stopWatch.isPaused())}VStack(对齐:.领先){文本(圈数").font(.title).填充()列表 {ForEach(self.stopWatch.laps.identified(by: \.uuid)) { (LapItem) in文本(LapItem.stringTime)}}}}}}

StopWatch.swift 视图文件来自

我只在添加最后一个 VStack 部分后收到此错误:

VStack(alignment: .leading) {文本(圈数").font(.title).填充()列表 {ForEach(self.stopWatch.laps.identified(by: \.uuid)) { (LapItem) in文本(LapItem.stringTime)}}}

我怀疑这可能是因为 List,我什至尝试在多个地方添加 Group{} 但它没有帮助并且在 StopWatch.swift 文件.我对 Swift 和 Xcode 还很陌生.为什么会发生这种情况,我该如何解决?

解决方案

我能够通过添加 id 字段来编译它.

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

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

I am trying to make a stopwatch app.

The code:

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)
                    }
                }
            }
        }
    }
}

The StopWatch.swift view file was from here.

I'm getting "Unable to infer complex closure return type; add explicit type to disambiguate" Error in the

struct ContentView : View {
    @ObservedObject var stopWatch = StopWatch()

    var body: some View {
        VStack {
            Text(self.stopWatch.stopWatchTime)
                .font(.custom("courier", size: 70))

part in the "VStack {" line

I'm only getting this error after adding the last VStack part:

VStack(alignment: .leading) {
                Text("Laps")
                    .font(.title)
                    .padding()

                List {
                    ForEach(self.stopWatch.laps.identified(by: \.uuid)) { (LapItem) in
                        Text(LapItem.stringTime)
                    }
                }
            }

I suspect it might be because of the List, I even tried adding Group{} on multiple places but it didn't help and couldn't find any fix in the StopWatch.swift file. I'm fairly new to Swift and Xcode.Why is this happening and how do I solve it?

解决方案

I was able to get it to compile by adding a id field.

identified(by:) is depreciated so you should now use init(_:id:): source

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

这篇关于"无法推断复杂的闭包返回类型;添加显式类型以消除歧义"SwiftUI 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:58