Post

Replies

Boosts

Views

Activity

Reply to Display of the last LAP
Your struct has the time as a String so you can either grab the last x characters from that String, or you can extend the LapTime struct to store the relevant seconds, and retrieve the lap seconds time whenever you need it (my preference). Here's the code, re-written to do that: import SwiftUI struct ContentView: View { 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 var body: some View { VStack{ Text(getTimeString(m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)) .font(.system(size: 60, design: .monospaced)) .frame(width: 300.0, height: 100.0) .onReceive(timer) {_ in if(self.running) { self.timerCalcs() } } Text(getTimeString(m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)) .font(.system(size: 60, design: .monospaced)) .frame(width: 300.0, height: 100.0) .onReceive(timer) {_ in if(self.running) { self.timerCalcs() } } Text("Lap \(lapCount - 1)") Text(self.lapTimes.last?.time ?? "") Text(self.lapTimes.last?.getLapSecondsString() ?? "") HStack{ Button(action: { 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 } }) { ZStack{ Circle().fill(Color.gray).frame(width: 100, height: 100) self.running ? Text("Lap").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) : Text("Reset").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) } } Spacer() Button(action: { self.running = !self.running }) { ZStack{ Circle().fill(self.running ? Color.red : Color.green).frame(width: 100, height: 100).font(.system(size: 20, design: .monospaced)) self.running ? Text("Stop").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) : Text("Start").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) } } }.padding() List{ LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds) ForEach(self.lapTimes.reversed()) { time in time } } } } 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("Lap \(num)").font(.system(size: 20, design: .monospaced)) 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)" } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } You still have your func getTimeString() which is used in a couple of places at the top, but you have a smaller version of it that only deals with the seconds and centiSeconds in that specific instance of a LapTime object. So, this line: Text(self.lapTimes.last?.getLapSecondsString() ?? "") says, "give me the last lap, then give me the lap seconds as a String made up of just the seconds and centiSeconds for that specific LapTime object." Note also how the init() func in the struct also generates the time value itself - there was no point in generating it then giving it to the object, when you could just get the object to generate it itself. time = getTimeString(m: minutes, s: seconds, c: centiSeconds). Hope this makes sense?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Display of the last LAP
You can do that by setting up some modelData, then saving the LapTimes into there. Your second view would then simply access the modelData. Do a search on these forums for modelData, or use your preferred search site.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Clearing activation lock returns 404
What code are you running? What's the address of the web resource? This is a 404 error, so it's not a code issue; it's a web issue - the resource cannot be found. What can we do about it? Also, please use correct tags for your query. iPadOS, macOS, iPhone and Enterprise are not really appropriate tags for your issue.
Dec ’22
Reply to Color of BackGround and Condition
It's because you've not told the view to update when something has changed. This works by setting the bgColour to green initially, then it's recalculated when the icons are tapped. Because bgColour is a @State var and it's changed, the background is redrawn. struct ContentView: View { @State var bgColour = Color.green @State var tpsRealise = 0 @State var tpsEstime = 0 var body: some View { ZStack { bgColour VStack { Text("Tps Réalisé : \(tpsRealise)") Image(systemName: "plus.square") .font(.title) .onTapGesture { tpsRealise += 1 bgColour = displayBackgroundColour() } Spacer() Text("Temps estimé : \(tpsEstime)") Image(systemName: "plus.square") .font(.title) .onTapGesture { tpsEstime += 1 bgColour = displayBackgroundColour() } } } } func displayBackgroundColour() -> Color { return tpsRealise > tpsEstime ? Color.red : Color.green } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Bug in sed
Try using LC_CTYPE=C: echo “hi | LC_CTYPE=C LANG=en_US.UTF-8 sed -e s'/^/x/g' If that doesn't help, then yes, raise a feedback report. There's. lot of useful info here: https://stackoverflow.com/questions/19242275/re-error-illegal-byte-sequence-on-mac-os-x/19770395#19770395
Topic: App & System Services SubTopic: Core OS Tags:
Dec ’22
Reply to hi team, I can not add card to apple wallet,
Is your inability to add a card to your Wallet anything to do with code, or is this a product support question? If the latter, please try the Apple Support forums. You're on the Developer forums where developers get together to discuss code and get fixes for code for their apps. This forum is nothing to do with product support.
Dec ’22
Reply to New iOS version notification
Apple stagger notification of releases so that they can a) handle the server load, and b) if any issues are found by those who first updated, Apple can pull the release so it doesn't affect anyone else. You are free to manually check for updates and request the update whenever you like.
Topic: App & System Services SubTopic: Core OS Tags:
Dec ’22
Reply to iWatch
It's not an "iWatch", it's an "Apple Watch", and no, steps are not synced to the Mac. You pair your Apple Watch with an iPhone, and the data is synced to that. You can't pair your Apple Watch with a Mac, so there's no link there.
Topic: App & System Services SubTopic: Core OS Tags:
Dec ’22