此 class 不符合键取消的键值编码

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

我不断收到此错误: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.'

我正在尝试完成 iOS 应用程序入门的 Apple 开发人员指南。我的代码和情节提要看起来与示例文件中的代码和情节提要完全一样。我希望新人能够看到我不是的东西?

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
    }
}

还有一些其他文件,但请告诉我您需要什么,因为我对 Swift/XCode 还很陌生,不确定要 provide/not 提供什么。

这意味着您的故事板上有一些东西连接到名为 cancel 的 IBOutlet,但您的 class 中没有这个 IBOutlet。所以编译器在你的 class 中找不到 Key cancel(这意味着 属性)。您应该在情节提要中找到该按钮(我认为它是一个 UIButton,因为名称),在其上单击鼠标右键并单击 "x" 以删除该连接。或者您可能想要完全删除此按钮。或者您可能想将此 IBOutlet 添加到您的 class.

我同意 Alex Shubin 在他的回答中提到的,一个可行的解决方案,如“寻找任何不再存在的连接并删除它们”。我遇到了同样的问题,他的建议修复了那个错误。对于编程新手,以及像我这样 Xcode 的人,了解更多有关如何在情节提要中定位 x 按钮的信息可能会有所帮助。

点击情节提要中视图控制器顶部的黄色圆圈。

然后在实用程序栏的右上角显示连接检查器。连接检查器是最右侧的菜单,一个带有向右箭头的圆圈。单击它,您应该会看到该视图控制器的所有连接。

我在我的应用程序中添加了一个屏幕截图。

检查 "Identity Inspector" 下的模块名称。模块名称必须 selected 或 "Inherit Module from Target" 必须检查。如果有多个模块,select合适的。