Good day all.
This is part of a tutorial on which we build an egg timer app.
When the app is run, depending on the egg hardness was pressed, it will countdown is seconds until the egg was ready for consumption depending how long said hardness was pressed.
However, at the moment, I have two issues which I'm struggling to understand is happening:
Firstly, when either hardness is selected, it will trigger the countdown but also it will trigger others simultaenously, e.g. if I tap 'Soft', countdown starts, but if I press 'Hard', that countdown starts also.
Secondly, the print is just displaying full values from my dictionary "eggTimers" instead of minus the value by 1 each second. I have tried
But I get "Left side of mutating operator isn't mutable: 'eggTimers' is a 'let' constant" so changing it to just '- 1' then removes the error.
My goal is so that there is one countdown at a time and on the console.
I then declated and created a function and called here (I commented my original scheduledTimer)
But the speedy countdown remains.
Any help?
This is part of a tutorial on which we build an egg timer app.
When the app is run, depending on the egg hardness was pressed, it will countdown is seconds until the egg was ready for consumption depending how long said hardness was pressed.
However, at the moment, I have two issues which I'm struggling to understand is happening:
Firstly, when either hardness is selected, it will trigger the countdown but also it will trigger others simultaenously, e.g. if I tap 'Soft', countdown starts, but if I press 'Hard', that countdown starts also.
Secondly, the print is just displaying full values from my dictionary "eggTimers" instead of minus the value by 1 each second. I have tried
Code Block self.eggTimers[hardness]! -= 1 }
But I get "Left side of mutating operator isn't mutable: 'eggTimers' is a 'let' constant" so changing it to just '- 1' then removes the error.
My goal is so that there is one countdown at a time and on the console.
Code Block import UIKit class ViewController: UIViewController { let eggTimers = ["Soft": 300, "Medium": 420, "Hard": 720] @IBAction func hardnessSelected(_ sender: UIButton) { // this is printed into console what the title is of the button is pressed let hardness = sender.currentTitle! //soft, med, hard Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in if self.eggTimers[hardness]! > 0 { print ("\(self.eggTimers[hardness]!) seconds left") self.eggTimers[hardness]! - 1 } else { Timer.invalidate() } } } }
I then declated and created a function and called here (I commented my original scheduledTimer)
Code Block Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(uptdateTimer), userInfo: nil, repeats: true) } @objc func uptdateTimer() { if secondsRemain > 0 { print("\(secondsRemain) seconds.") secondsRemain -= 1 }
But the speedy countdown remains.
Any help?