This class is not key value coding-compliant for the key cancel
Asked Answered
N

3

22

I keep getting this error: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FoodTracker.MealViewController 0x7faa9ed189d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cancel.'

I'm trying to complete the Apple developer guide to getting started with iOS apps. My code and storyboard looks exactly like theirs does in the example file. I'm hoping that a fresh eye might be able to see something I am not?

import UIKit
import os.log

class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var photoImageView: UIImageView!
    @IBOutlet weak var ratingControl: RatingControl!
    @IBOutlet weak var saveButton: UIBarButtonItem!
    /*
     This value is either passed by 'MealTableViewController' in
     'prepare(for:sender) or constructed as part of adding a new meal.
    */
    var meal: Meal?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Handle the text field's user input through delegate callbacks
        nameTextField.delegate = self

        // Enable save button only if text field has valid Meal name
        updateSaveButtonState()
    }

    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        updateSaveButtonState()
        navigationItem.title = textField.text
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        // Disable save button while editing
        saveButton.isEnabled = false
    }

    //MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        // Set photoImageView to display the selected image
        photoImageView.image = selectedImage
        // Dismiss the picker
        dismiss(animated: true, completion: nil)
    }

    //MARK: Navigation
    @IBAction func cancel(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }


    // Configure view controller before it's presented
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)

        // Configure destination view controller only when save button pressed
        guard let button = sender as? UIBarButtonItem, button === saveButton else {
            os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
            return
        }

        let name = nameTextField.text ?? ""
        let photo = photoImageView.image
        let rating = ratingControl.rating

        // Set meal to be passed to MealTableViewController after unwind segue
        meal = Meal(name: name, photo: photo, rating: rating)
    }

    //MARK: Actions
    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        // Hide the keyboard
        nameTextField.resignFirstResponder()
        // UIImagePickerController is a view controller that lets a user pick media from their photo library
        let imagePickerController = UIImagePickerController()
        // Only allow photos to be picked, not taken
        imagePickerController.sourceType = .photoLibrary
        // Make sure ViewController is notified when the user picks an image
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }

    //MARK: Private Methods
    private func updateSaveButtonState() {
        // Disable the save button if the text field is empty
        let text = nameTextField.text ?? ""
        saveButton.isEnabled = !text.isEmpty
    }
}

There are a few other files, but please just let me know what you need because I am very new to Swift/XCode and not sure what to provide/not provide.

Nell answered 27/8, 2017 at 7:27 Comment(2)
Delete the existing cancel IBAction and drag and drop again the cancel action in class. because it's not connected with storyboard somehow.Prynne
I had the same issue. It's happened because I control + dragged the cancel button into the Properties section first, just as all the previous steps had me do. When I realized I was supposed to control + drag this view to the Navigation section, I deleted the outlet in MealViewController. This didn't delete the link from the storyboard, which is causing the crash since the outlet doesn't exist now. You need to delete the link using the steps outlined in the answers belowOvermeasure
L
54

This means that you have something on your storyboard connected to the IBOutlet called cancel but you don't have this IBOutlet in your class. So compiler can't find Key cancel(it means property) in your class. You should find that button(i think it's a UIButton because of the name) in storyboard, click right mouse button on it and click "x" to delete that connection. Or you might want to delete this button at all. Or you might want to add this IBOutlet to your class.

Lath answered 27/8, 2017 at 7:36 Comment(1)
Best answer! ThanksSemiaquatic
S
19

I agree with what Alex Shubin mentioned in his answer, an actionable solution like “look for any connections that don’t exist anymore and delete them”. I was having the same issue and his suggestion fixed that bug. For those who are new to programming, and Xcode like me, a little more information on how to locate the x button in storyboard might be helpful.

Click on the yellow circle at the top of the view controller in storyboard.

enter image description here

Then show the connections inspector in the upper right of the utilities bar. The connections inspector is the menu on the far right, a circle with a right pointing arrow. Click that, and you should see all the connections for that view controller.

I'm including a screen shot of what that looks like in my app.

enter image description here

Shores answered 6/2, 2018 at 22:2 Comment(0)
S
10

Check for the module name under "Identity Inspector". Either module name has to be selected or "Inherit Module from Target" has to be checked. If there are multiple modules, select the appropriate one.

Satisfied answered 18/5, 2018 at 21:16 Comment(3)
My last view controller randomly didn't have this selected and its been driving me crazy trying to figure out why my simple IBoutlets to labels were crashing. Thank you!Active
The "Inherit Module from Target" solved it. Thank you! This was killing me for hours!Kelsey
This saved my bacon. I my case, I had a storyboard that I'd copied in from another project, and I'd double-checked to verify that all IBOutlets were connected OK. After setting "Inherit..." for each scene's view controller, the error disappeared.Cressy

© 2022 - 2024 — McMap. All rights reserved.