0

I'm trying to pass a number from a label to a timer.

override func viewDidLoad() {
    super.viewDidLoad()
    pauseButton.isEnabled = false
    seconds = Int(timerTimeSetting.text!)! //Here is my problem line
    if let task = timerTask {
        timerTaskName.text = task.task
    }

    if let timerTimeLeft = timerTime {
        timerTimeSetting.text = timerTimeLeft.time
    }

    self.timerLabel.text = timeString(time: TimeInterval(seconds))

}

I get a "found nil" error. Below is how I initialize the seconds variable.

var seconds: Int = 0
Tom
  • 81
  • 10
  • Can you show me a result of print(timerTimeSetting?.text). Int class has fail-able initializer that accept string value and try to change it to Int. Getting nil back mean by it failed the operation. So if the label text has format like 00:00:00, then you may want to split those using "components(":")" and cover to int accordingly. – Bubu Jul 21 '17 at 22:57
  • I did this and it prints Optional("10")...I can safely unwrap but I still get nil – Tom Jul 22 '17 at 13:11
  • so I ended up successfully converting to an integer like so let timeLeft: Int? = Int(timerTimeSetting.text!) – Tom Jul 22 '17 at 13:56
  • I'm not sure how to write code in comments – Tom Jul 22 '17 at 13:56
  • That is because Int(String) return Int? type. Did you check the value inside timeLeft was not nil? If worked then all good! – Bubu Jul 22 '17 at 17:31
  • I figured it out but thank you – Tom Jul 22 '17 at 23:40

0 Answers0