skip to Main Content

Here is all of my code, as said in the title there is an Error occurring when I try to run my app I’m not sure what I can do to fix it. How can I fix this I have tried a couple of things that have not worked.

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]
    var timer = Timer()
    var player: AVAudioPlayer!
    var totalTime = 0
    var secondsPassed = 0
    
    @IBAction func coffeeSelected(_ sender: UIButton) {
    
    timer.invalidate()
        let coffee = sender.currentTitle!
        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: "mixkit-warnin...uzzer-991(1)")
            player = try! AVAudioPlayer(contentsOf: url!)
            player.play()
        }
    }
}

2

Answers


  1. That error almost always means that you have a broken IBOutlet link in a Storyboard or XIB. (The storyboard points to an outlet that doesn’t exist in the class it points to.)

    Given that the error mentions an IBOutlet called "tittleLabel" and your code has an outlet called "titleLabel", that seems like a likely cause of your crash.

    I suggest opening the Storyboard that contains your ViewController class, and examining all the outlets carefully using the connections inspector. If you find the bad one, delete it and then control-drag from your code back to the outlet in the storyboard to connect the correct outlet.

    Login or Signup to reply.
  2. This type of error comes when there is broken connection in storyboard or XIB file. In storyboard or Xib, select your view controller and check your connection inspector, just like in the following picture:

    storyboard image to check connections

    Check here the connection of titleLabel, first remove the connection and again make its connection with your swift class file.

    Hope this works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search