Home > Net >  every time i try to run my app it crashes and gives me this Error, "this class is not key value
every time i try to run my app it crashes and gives me this Error, "this class is not key value

Time:01-02

import UIKit
import AVFoundation

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

    //'this is the code that i am using for my project'

    
    @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()
        }
    }
}

"this is the code that i am using for my project."

CodePudding user response:

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.

CodePudding user response:

This type of error comes when there is some issue in label's or button's connection. In storyboard, 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.

  • Related