-2

This is the code and it's giving me an Error from let coffee = sender.currentTitle! that's my problem.

I'm trying to make my button be the name of the button which is Decaf but when i press it it gives me that Error.

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    override func viewDidLoad() {
       super.viewDidLoad()
        // Do any additional setup after loading the view.
    
}


    
    @IBOutlet weak var progressBar: UIProgressView!
       
    @IBOutlet weak var titleLabel: UILabel!
    
    let coffeeTimes = ["Decaf": 5, "coffee": 5]
    var timer = Timer()
    var player: AVAudioPlayer!
    var totalTime = 0
    var secondsPassed = 0
    
    @IBAction func coffeeSelected(_ sender: UIButton) {
       
        timer.invalidate()
        let coffee = sender.currentTitle! //1.This is the line were its giving me the Error"
        totalTime = coffeeTimes[coffee]!
        
        progressBar.progress = 0.0
        secondsPassed = 0
        titleLabel.text = coffee



        timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: #selector(updateTimer), userInfo:nil, repeats: true)
    }
    
    @objc func updateTimer() {
        if secondsPassed < totalTime {
            secondsPassed += 1
            progressBar.progress = Float(secondsPassed) / Float(totalTime)
            print(Float(secondsPassed) / Float(totalTime))
        } else {
            timer.invalidate()
            titleLabel.text = "check coffee"
            
            let url = Bundle.main.url(forResource: "alarm_sound", withExtension: "mp3")
            player = try! AVAudioPlayer(contentsOf: url!)
            player.play()
        }
    }
}
  • Could you print `sender`? – Larme Jan 01 '22 at 16:45
  • 1
    If you look at the documentation for `currentTitle`, you'll see, "The value may be nil". Maybe you should test it instead of telling the app to crash if it's nil. – Phillip Mills Jan 01 '22 at 16:59
  • how would i do that, Im new to swift – Kaleb Zimmerman Jan 01 '22 at 17:13
  • @Kaleb "Unexpectedly found nil while unwrapping an Optional value" the message tells the reason for crash. In your code, you are trying to read sender's currentTitle by using force unwrap sender.currentTitle! When you use force unwrap (!) you instruct the compiler that there will be a value available, at runtime. However, in our case here, currentTitle may be nil and when it is nil the app crashes at runtime. You should use optional binding to check for a value first then access it. if let currentTitle = sender.currentTitle { titleLable.text = currentTitle } – singhabhi13 Jan 01 '22 at 17:26
  • ok, how would i use optional binding to check for a value then access it. – Kaleb Zimmerman Jan 01 '22 at 17:32

1 Answers1

1

Use optional binding so the app doesn't crash. This if let statement will only run if "currentTitle" actually exists, in your case, it doesn't, so there must be some other problem in your code.

if let coffee = sender.currentTitle {
    totalTime = coffeeTimes[coffee]
    titleLabel.text = coffee
    progressBar.progress = 0.0
    secondsPassed = 0
}

An alternative is to return from the whole function if the value doesn't exist. Don't use both at the same time.

guard let coffee = sender.currentTitle else { return }

Tad
  • 889
  • 9
  • 23