Skip to content

Instantly share code, notes, and snippets.

@deathlezz
Last active November 1, 2021 21:23
Show Gist options
  • Save deathlezz/9ef790922c61b2cfff40de6a089fbf50 to your computer and use it in GitHub Desktop.
Save deathlezz/9ef790922c61b2cfff40de6a089fbf50 to your computer and use it in GitHub Desktop.
Timer App in Swift 5.
//
// Timer App
//
import Foundation
// time left to finish
var (hours, minutes, seconds) = (1, 0, 3)
// convert hours, minutes and seconds to seconds and add them together
var convert = hours * 3600 + minutes * 60 + seconds
// function that convert seconds to hours, minutes and seconds
func secondsToHrsMinSec(seconds: Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, seconds % 60)
}
while convert != 0 {
// call the function
let (h, m, s) = secondsToHrsMinSec(seconds: convert)
// set the timer label format (hh:mm:ss)
let format = String(format: "%02i:%02i:%02i", h, m, s)
print(format)
// one second delay
sleep(1)
// subtract one second
convert -= 1
}
print("Ring, Ring")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment