Compare 2 values

Hello, on my stopwatch I want to compare a time in seconds and tenths recovered in a Picker with the last Lap made to change foregroundColor. I got an error like "Type '()' cannot conform to 'View'" when I make this calcul : comparaisonTemps = tempsTour <= lapTourCompare

Do you see the problem? Format of the tempsTour and lapTourCompare ? Thank you in advance for your help

    
    let timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
    @State var minutes  = 0
    @State var seconds = 0
    @State var centiseconds = 0
    @State var running = false
    @State var lapTimes: [LapTime] = []
    @State var lapCount = 1
    @State var lapMinutes = 0
    @State var lapSeconds = 0
    @State var lapCentiseconds = 0
    
    @State var secondesSelection = 18
    @State var dixiemesSelection = 0
    
    @State var comparaisonTemps = false
    @State var tempsTour = 0
    @State var lapTourCompare = 0
    
    
    let secondes = Array(10..<60)
    let dixiemes = Array(0..<10)
    
    
    
    var body: some View {
        
        GeometryReader { geo in
            
            HStack {
                ZStack {
                    Rectangle()
                        .fill(Color.green.opacity(0.0))
                        .frame(width: UIScreen.main.bounds.width * 0.15)
                    VStack {
                        ZStack {
                            Rectangle()
                                .fill(Color.blue.opacity(0.0))
                                .frame(width: UIScreen.main.bounds.width * 0.15, height: UIScreen.main.bounds.height * 0.25)
                            VStack {
                                Text("Tps prévu au tour")
                                    .font(.system(size: 20, design: .monospaced))
                                HStack {
                                    Picker("Secondes", selection: $secondesSelection) {
                                        ForEach(secondes, id: \.self) {
                                            Text("\($0)''")
                                                .font(.system(size: 20, design: .monospaced))
                                                .fontWeight(.semibold)
                                            
                                        }
                                    }
                                    
                                    Picker("Dixièmes", selection: $dixiemesSelection) {
                                        ForEach(dixiemes, id: \.self) {
                                            Text("\($0)")
                                                .font(.system(size: 20, design: .monospaced))
                                                .fontWeight(.semibold)
                                        }
                                    }
                                }
                                .pickerStyle(.wheel)
                                .frame(width: 140)
                            }
                        }
                        ZStack {
                            Rectangle()
                                .fill(Color.pink.opacity(0.0))
                                .frame(width: UIScreen.main.bounds.width * 0.15, height: UIScreen.main.bounds.height * 0.6)
                            VStack {
                                Text(getTimeString(m: self.minutes, s: self.seconds, c: self.centiseconds))
                                    .font(.system(size: 20, design: .monospaced))
                                    .onReceive(timer) {_ in
                                        if(self.running) {
                                            self.timerCalcs()
                                        }
                                    }
                                List {
                                    LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)
                                    ForEach(self.lapTimes.reversed()) { time in time }
                                }.frame(width: UIScreen.main.bounds.width * 0.15)
                            }
                        }
                        Button(action: {
                            self.running = !self.running
                        })
                        {
                            ZStack {
                                
                                
                                Rectangle()
                                    .fill(Color.indigo.opacity(0.0))
                                    .frame(width: UIScreen.main.bounds.width * 0.15)
                                
                                
                                Circle().fill(self.running ? Color.red : Color.green).frame(height: 100).font(.system(size: 30, design: .monospaced))
                                
                                self.running ? Text("Stop").foregroundColor(Color.white).font(.system(size: 30, design: .monospaced)) : Text("Start").foregroundColor(Color.white).font(.system(size: 30, design: .monospaced))                    }
                        }
                    }
                }


Answered by Claude31 in 739090022

Unfortunately, I cannot test as we don't have complete code. We don't even know on which Rectangle onTapGesture applies.

Did you test what I proposed ?

                        .onTapGesture {
                            if !self.running {
                                self.minutes = 0
                                self.seconds = 0
                                self.centiseconds = 0
                                self.lapTimes = []
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                                self.lapCount = 1
                            } else {
                                self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds))
                                self.lapCount += 1
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                            }
                            let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
                            let lapTourCompare = Double(seconds) + Double(centiseconds)
                            comparaisonTemps = tempsTour <= lapTourCompare
                       }

What do you get when doing this ?

.

How would I see the result returned by `let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)

Where do you want it displayed ?

                ZStack {
                    Rectangle()
                        .foregroundColor(comparaisonTemps ? Color.red.opacity(0.5) : Color.green.opacity(0.5))
                        .onTapGesture {
                            if(!self.running) {
                                self.minutes = 0
                                self.seconds = 0
                                self.centiseconds = 0
                                self.lapTimes = []
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                                self.lapCount = 1
                            } else {
                                self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds))
                                self.lapCount += 1
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                            }
                        }
                    let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
                    let lapTourCompare = Double(seconds) + Double(centiseconds)
                    //                  comparaisonTemps = tempsTour <= lapTourCompare
                    
                    VStack {
                        ZStack {
                            Rectangle()
                                .fill(Color.brown.opacity(0.0))
                                .frame(height: UIScreen.main.bounds.height * 0.3)
                            Text("\(lapCount - 1)")
                                .font(.system(size: min(geo.size.height, geo.size.width) * 0.40))
                                .fontWeight(.bold)
                                .frame(height: UIScreen.main.bounds.height * 0.25)
                                .foregroundColor(.red)
                        }
                        ZStack {
                            Rectangle()
                                .fill(Color.yellow.opacity(0.0))
                                .frame(height: UIScreen.main.bounds.height * 0.65)
                            Text(self.lapTimes.last?.getLapSecondsString() ?? "")
                                .font(.system(size: min(geo.size.height, geo.size.width) * 0.40))
                                .fontWeight(.bold)
                                .frame(height: UIScreen.main.bounds.height * 0.38)
                        }
                    }
                    
                    
                    
                    
                }
            }
        }
    }
    
    func timerCalcs() {
        if(self.centiseconds < 99) {
            self.centiseconds += 1
            
        } else {
            self.centiseconds = 0
            if(self.seconds < 59) {
                self.seconds += 1
                
            } else {
                self.seconds = 0
                self.minutes += 1
            }
        }
        
        if(self.lapCentiseconds < 99) {
            self.lapCentiseconds += 1
            
        } else {
            self.lapCentiseconds = 0
            if(self.lapSeconds < 59) {
                self.lapSeconds += 1
                
            } else {
                self.lapSeconds = 0
                self.lapMinutes += 1
            }
        }
    }
}

func getTimeString(m: Int, s: Int, c: Int) -> String {
    var centiString = String(c)
    var secString = String(s)
    var minString = String(m)
    if(c < 10) {
        centiString = "0\(c)"
    }
    if(s < 10) {
        secString = "0\(s)"
    }
    if(m < 10) {
        minString = "0\(m)"
    }
    return "\(minString):\(secString).\(centiString)"
}

struct LapTime: View, Identifiable {
    let id = UUID()
    let num: Int
    let minutes: Int
    let seconds: Int
    let centiSeconds: Int
    let time: String
    
    var body: some View {
        HStack {
            Text("\(num)").font(.system(size: 20, design: .monospaced)) //Lap
            Spacer()
            Text(time).font(.system(size: 20, design: .monospaced))
        }
    }
    
    init(n: Int, m: Int, s: Int, c: Int) {
        num = n
        minutes = m
        seconds = s
        centiSeconds = c
        time = getTimeString(m: minutes, s: seconds, c: centiSeconds)
    }
    
    func getLapSecondsString() -> String {
        var centiString = String(centiSeconds)
        var secString = String(seconds)
        if(centiSeconds < 10) {
            centiString = "0\(centiSeconds)"
        }
        if(seconds < 10) {
            secString = "0\(seconds)"
        }
        return "\(secString).\(centiString)"
    }
}

Screenshot

I got an error like "Type '()' cannot conform to 'View'" when I make this calcul : comparaisonTemps = tempsTour <= lapTourCompare

In SwiftUI, you can declare a const in the view (here ZStack)

                    let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
                    let lapTourCompare = Double(seconds) + Double(centiseconds)

But you cannot assign a value to a var, unless it is in a modifier closure for instance

                 comparaisonTemps = tempsTour <= lapTourCompare

A simple change would be to do it inside the onTap

                        .onTapGesture {
                            if !self.running {
                                self.minutes = 0
                                self.seconds = 0
                                self.centiseconds = 0
                                self.lapTimes = []
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                                self.lapCount = 1
                            } else {
                                self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds))
                                self.lapCount += 1
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                            }
                            let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
                            let lapTourCompare = Double(seconds) + Double(centiseconds)
                            comparaisonTemps = tempsTour <= lapTourCompare
                       }

.

As for the picker, what do you expect to see in it ?

                                HStack {
                                    Picker("Secondes", selection: $secondesSelection) {
                                        ForEach(secondes, id: \.self) {
                                            Text("\($0)''")
                                                .font(.system(size: 20, design: .monospaced))
                                                .fontWeight(.semibold)
                                            
                                        }
                                    }
                                    
                                    Picker("Dixièmes", selection: $dixiemesSelection) {
                                        ForEach(dixiemes, id: \.self) {
                                            Text("\($0)")
                                                .font(.system(size: 20, design: .monospaced))
                                                .fontWeight(.semibold)
                                        }
                                    }
                                }
                                .pickerStyle(.wheel)
                                .frame(width: 140)
                            }
  • First Picker is either not wide enough (use 160 vs 140) or font is too large (use 16 instead of 20)
  • second, you don't show where and how dixiemesSelection is set, so impossible to say.

Question: why do secondes start at 10 ?

Thanks for your help. No problem with the picker. it recovers seconds and tenths. Example 15"8 would be a lap time to aim for for the athlete and I have to compare it with his last Lap. If his Lap is higher the Rectangle() is red otherwise it is green. The declarations are already in the Zstack, how should I to do with the calcul comparisonTemps? How would I see the result returned by `let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)

                    let lapTourCompare = Double(seconds) + Double(centiseconds)`

Accepted Answer

Unfortunately, I cannot test as we don't have complete code. We don't even know on which Rectangle onTapGesture applies.

Did you test what I proposed ?

                        .onTapGesture {
                            if !self.running {
                                self.minutes = 0
                                self.seconds = 0
                                self.centiseconds = 0
                                self.lapTimes = []
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                                self.lapCount = 1
                            } else {
                                self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds))
                                self.lapCount += 1
                                self.lapMinutes = 0
                                self.lapSeconds = 0
                                self.lapCentiseconds = 0
                            }
                            let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
                            let lapTourCompare = Double(seconds) + Double(centiseconds)
                            comparaisonTemps = tempsTour <= lapTourCompare
                       }

What do you get when doing this ?

.

How would I see the result returned by `let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)

Where do you want it displayed ?

I did the test, I no longer have the message, thank you. I wanted to know the result of my 2 calculations because whatever the Lap the red color applies.

Where I can depose the code, it is longer for here?

Si vous voulez laisser un mail ici pour quelques instants, on pourra échanger des fichiers par mail.

Compare 2 values
 
 
Q