format - How to do a CountDown timer using Swift -


hello developing countdown timer,which has stop after 8 hours. here is:

import uikit  class viewcontroller: uiviewcontroller {     var timer = nstimer()    var counter = 0  @iboutlet weak var label: uilabel!  override func viewdidload() { super.viewdidload()   // additional setup after loading view, typically   nib. }   override func didreceivememorywarning() { super.didreceivememorywarning()   // dispose of resources can recreated.  }  @ibaction func start(sender: anyobject) { counter = 28800 label.text = string(counter) timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: selector("updatetimer"), userinfo: nil, repeats: true)      }    func updatetimer () {  counter -= 1 label.text = string(counter)   }   @ibaction func stop(sender: anyobject) {  timer.invalidate()  }  } 

but in stead of having 28800 seconds (which 8hours)i want display 08:00:00 , 07:59:59 , 07:59:58, on... @ moment showing 28800, 28799, 28798, on. have idea how format real clock (08:00:00)?

i had same problem , pretty noob in xcode/swift - after reviewing several sources, here's put works me:

replace your:

func updatetimer () {  counter -= 1 label.text = string(counter) } 

with this:

func updatetimer () {         counter -= 1         let hours = int(counter) / 3600         let minutes = int(counter) / 60 % 60         let seconds = int(counter) % 60         label.text = string(format: "%02i:%02i:%02i",hours,minutes,seconds) } 

(xcode 8.1 warns use of "let" or "var" hours/minutes/seconds, still works)

result like:

00:00:00 

Comments