UnwantedTachyon
UnwantedTachyon

Reputation: 25

"Unable to infer complex closure return type; add explicit type to disambiguate" Error in SwiftUI

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

Code Error

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?

Upvotes: 0

Views: 506

Answers (1)

sfung3
sfung3

Reputation: 2377

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

Upvotes: 1

Related Questions