Search code examples
iosswiftavfoundationsegueavkit

Segue error: due to uncaught exception 'NSUnknownKeyException'


I am trying to turn on Segue by pressing a button. Before I added this code it worked but after adding the code (code that triggers a video by clicking on a button) the code crashes my application with the following error:

due to uncaught exception 'NSUnknownKeyException reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fatB.

I searched for the error and I understand that its mean that Xcode cant find some name (fatB) But I can't figure the rest out.

Below is my code:

viewcontroller1 code:

import UIKit

class ViewController: UIViewController {
    @IBAction func movieB(_ sender: Any) {
        performSegue(withIdentifier: "firstsegue", sender: self)
    }

    @IBOutlet weak var winnerLabel: UILabel!
}

viewcontroller2 code:

import UIKit
import AVKit
import AVFoundation

class ViewController2: UIViewController {

    func forbutton(name : String, type : String)  {
        func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            playVideo(name: name, type: type)
        }
    }

    private func playVideo(name : String, type : String) {
        guard let path = Bundle.main.path(forResource: "\(name)", ofType:"\(type)") else {
            debugPrint("video not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()
        playerController.player = player
        present(playerController, animated: true) {
            player.play()
        }
    }        

    @IBAction func fatB(_ sender: Any) {
        forbutton(name: "fatB", type: "mp4")
    }

    @IBAction func coderB(_ sender: Any) {
        forbutton(name: "birdC", type: "mp4")
    }

    @IBAction func clashB(_ sender: Any) {
        forbutton(name: "clash", type: "mp4")
    }
}

Solution

  • It may be because:

    1) You have removed a IBOutlet from your code but not from Controller's scene in Storyboard.

    2) You may have changed the name of your IBOutlet reference variable.

    Please check your Controller's scene in Storyboard and remove the unused IBOutlet reference or update it with the new reference variable name.