0

I want to have my stepper be used to add minutes to a timer. The stepper outputs + or - 1 by default, i am then using *60 to make it a minute, but this makes my timer display the total number of seconds in my timer rather than minutes. e.g. pressing it twice reads 120 in the time rather than 02:00, How would I convert this?

@IBAction func restStepperValueChanged(_ sender: UIStepper) {
    numberOfRestLabel.text = Int(sender.value).description
    restCount = Int(sender.value)*60
    restRemainingCountdownLabel.text = String(restCount)
}
infernouk
  • 1,137
  • 4
  • 13
  • 21

2 Answers2

0

This?

let seconds = restCount % 60
let minutes = (restCount / 60) % 60
let result = "\(minutes):\(seconds)"
print(result)
Rob
  • 2,649
  • 3
  • 27
  • 34
0

restCount contains the number of seconds as it is set here:

restCount = Int(sender.value)*60

For example:

if sender.value = 1 then Int(1)*60 == 1*60 == 60

if sender.value = 2 then Int(2)*60 == 2*60 == 120

This means that this line is setting the number of seconds, not the number of minutes:

restRemainingCountdownLabel.text = String(restCount)

To get the time in the format of 02:00 you will need to manually construct a string using a format specifier:

String(format: "%02d:%02d", minutesValue, secondsValue)

An explaination:

String(format:) constructs a string according to the given format string. The format string here is "%02d:%02d". %d is the format specifier for integer values and modifying it to %02d specifies two digits with an optional leading zero. Eg '01' or '12'. We use this format specifier twice and place a colon between them. and then pass in integer values for the first and second format specifier.

BergQuester
  • 6,167
  • 27
  • 39