Eth*_*han 18 xcode sigabrt ios swift
我一直收到这个错误: 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.'
我正在尝试完成Apple开发人员指南以开始使用iOS应用程序.我的代码和故事板看起来与他们在示例文件中的完全一样.我希望新鲜的眼睛能够看到我不是的东西?
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
}
}
Run Code Online (Sandbox Code Playgroud)
还有一些其他文件,但请让我知道你需要什么,因为我对Swift/XCode很新,不知道提供什么/不提供什么.
Ale*_*bin 42
这意味着您的故事板上有一些连接到IBOutlet的故事板,cancel但您的班级中没有此IBOutlet.所以编译器cancel在你的类中找不到Key (它意味着属性).您应该在故事板中找到该按钮(我认为它是UIButton,因为名称),单击鼠标右键并单击"x"删除该连接.或者您可能想要删除此按钮.或者您可能希望将此IBOutlet添加到您的班级.
lla*_*orn 16
我同意Alex Shubin在他的回答中提到的一个可操作的解决方案,比如"寻找任何不存在的连接并删除它们".我遇到了同样的问题,他的建议修复了这个错误.对于那些刚接触编程的人和像我这样的Xcode,有关如何在故事板中找到x按钮的更多信息可能会有所帮助.
单击故事板中视图控制器顶部的黄色圆圈.
然后在实用程序栏的右上角显示连接检查器.连接检查器是最右侧的菜单,带有右箭头的圆圈.单击它,您应该看到该视图控制器的所有连接.
我在我的应用程序中包含了一个屏幕截图.