Search code examples
swiftcountdowntimer

Implement a dictionary value into a count down timer


I’m making an appllication quiz in which i want to implement the value of the dictionary to be the intial value of the countdown timer so if the "soft" key is targeted the count down timer starts from 300 instead of 60

import UIKit

class ViewController: UIViewController {
    let eggTimes :[String:Int]=["Soft":300,"Medium":420,"Hard":720
    ]
  var count: Int = 60
    
    func startCountDown() {
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
    }
    @objc func updateCounter() {
        if count>0 {
            count-=1
            countDownLabel.text = "\(count)"
            print(count)
        }
    }
    
    @IBOutlet weak var countDownLabel: UILabel!
    @IBAction func hardnessSelected(_ sender: UIButton) {
        startCountDown()
        let hardness=sender.currentTitle!
        let result = eggTimes[hardness]!
        print(result)
        
      
       
       
   
    }
   
    
     
    
} ```

Solution

  •     let hardness=sender.currentTitle!
        count = eggTimes[hardness]!
        startCountDown()