带有核心数据未捕获的 NSExeption 类型异常的 SIGABRT

Posted

技术标签:

【中文标题】带有核心数据未捕获的 NSExeption 类型异常的 SIGABRT【英文标题】:SIGABRT with coredata uncaught exception of type NSExeption 【发布时间】:2016-03-04 18:51:38 【问题描述】:

我正在制作一个使用核心数据的应用。我正在尝试制作一个名为 saveButton 的按钮,它将文本保存在几个文本字段中。我的核心数据类是 SecondVCItem,当我点击保存按钮时,应用程序崩溃并出现以下错误:

“2016-03-04 12:26:23.799 OCP[664:12324] Interface Builder 文件中的未知类 _TtC3OCP9ImageView。 2016-03-04 12:26:50.552 OCP[664:12324] CoreData:错误:无法调用 NSManagedObject 类“OCP.SecondVCItem”上的指定初始化程序 2016-03-04 12:26:50.559 OCP[664:12324] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“在实体中找不到密钥路径名称” 以 NSException 类型的未捕获异常终止"

奇怪的是,我没有在情节提要中两次分配任何东西,也没有不良的出口。我不知道为什么我不能保存这些文本字段。任何信息都是有帮助的。谢谢

import UIKit
import CoreData

class secondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

@IBOutlet var recTextField1: UITextField!
@IBOutlet var recTextField2: UITextField!
@IBOutlet var recTextField3: UITextField!
@IBOutlet var recButton1: UIButton!
@IBOutlet var recButton2: UIButton!
@IBOutlet var recButton3: UIButton!

@IBOutlet var editLocationButtonTapped: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet var essayNumber: UITextField!
@IBOutlet var letterOrForm: UISegmentedControl!
@IBOutlet var requiredTesting: UITextField!
@IBOutlet var commonApp: UISegmentedControl!
@IBOutlet var imageView: UIImageView!
var imagePicker = UIImagePickerController()

var selectedCollege : Item!

@IBOutlet var locationEditingTextField: UITextField!
@IBOutlet weak var dateTextField: UITextField!
@IBOutlet weak var datePicker: UIDatePicker!


let coreDataDB = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var items = [SecondVCItem]()


override func viewDidLoad()

    super.viewDidLoad()

    imagePicker.delegate = self

    dateTextField.enabled = false

    locationEditingTextField.text = selectedCollege.location

    if let collegeImage = selectedCollege.image 
        let collegeImageUI = UIImage(data: collegeImage)
        imageView.image = collegeImageUI
    

    let request = NSFetchRequest(entityName: "SecondVCItem")
    var results : [AnyObject]?

    do 
        results = try coreDataDB!.executeFetchRequest(request)
     catch 
        results = nil
    

    if results != nil 
        self.items = results as! [SecondVCItem]
    


    navigationItem.title = selectedCollege.name

    locationEditingTextField.enabled = false

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)








@IBAction func saveButton(sender: UIButton)

    let newItem = SecondVCItem(teacherRec1: recTextField1.text!, teacherRec2: recTextField2.text!, teacherRec3: recTextField3.text!, username: usernameTextField.text!, password: passwordTextField.text!, datepicker: datePicker, datepickertextfield: dateTextField.text!, reqTesting: requiredTesting.text!, numberOfEssays: essayNumber.text!, letterOrForm: letterOrForm.selectedSegmentIndex, commonApp: commonApp.selectedSegmentIndex, image: UIImage(named: "Question"), location: locationEditingTextField.text!, inManagedObjectContext: self.coreDataDB!)

    self.items.append(newItem)

    // CoreData save

    newItem.username = usernameTextField.text!
    newItem.password = passwordTextField.text!
    newItem.teacherRec1 = recTextField1.text!
    newItem.teacherRec2 = recTextField2.text!
    newItem.teacherRec3 = recTextField3.text!
    newItem.reqTesting = requiredTesting.text!
    newItem.numberOfEssays = essayNumber.text!
    newItem.datepickertextfield = dateTextField.text!

    usernameTextField.text! = newItem.username!
    passwordTextField.text! = newItem.password!
    recTextField1.text! = newItem.teacherRec1!
    recTextField2.text! = newItem.teacherRec2!
    recTextField3.text! = newItem.teacherRec3!
    requiredTesting.text! = newItem.reqTesting!
    essayNumber.text! = newItem.numberOfEssays!
    dateTextField.text! = newItem.datepickertextfield!





    newItem.save(self.coreDataDB!)


    // Reload Coredata data
   // self.items = SecondVCItem().fetchAll(self.coreDataDB!)
    self.items = SecondVCItem().fetchAll(self.coreDataDB!)

    // Reload TableView
    //self.collegeTableView.reloadData()


   

@IBAction func textFieldEditingDate(sender: UITextField)

    let datePicker : UIDatePicker = UIDatePicker()
    datePicker.datePickerMode = UIDatePickerMode.Date
    sender.inputView = datePicker
    datePicker.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)


@IBAction func datePicker(sender: UIDatePicker)

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle

    dateTextField.text = dateFormatter.stringFromDate(sender.date)


@IBAction func onTappedTeacherRec(sender: UIButton)

    let recAlert = UIAlertController(title: "Add Teacher Recs", message: nil, preferredStyle: UIAlertControllerStyle.Alert)

        recAlert.addTextFieldWithConfigurationHandler  (textField) -> Void in
            textField.placeholder = "Add First Teacher Name"
    
        recAlert.addTextFieldWithConfigurationHandler  (textField) -> Void in
            textField.placeholder = "Add Second Teacher Name"
    
        recAlert.addTextFieldWithConfigurationHandler  (textField) -> Void in
            textField.placeholder = "Add Third Teacher Name"
    
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
    recAlert.addAction(cancelAction)

    let addAction = UIAlertAction(title: "Add Teachers", style: .Default)
     (action) -> Void in
        let firstTeacherField = (recAlert.textFields?[0])! as UITextField
        let secondTeacherField = (recAlert.textFields?[1])! as UITextField
        let thirdTeacherField = (recAlert.textFields?[2])! as UITextField

        if firstTeacherField.text == ""
        
            self.recTextField1.hidden = true
            self.recButton1.hidden = true
        
        else
        
            self.recTextField1.hidden = false
            self.recTextField1.text = firstTeacherField.text
            self.recButton1.hidden = false
        
        if  secondTeacherField.text == ""
        
            self.recTextField2.hidden = true
            self.recButton2.hidden = true
        
        else
        
            self.recTextField2.hidden = false
            self.recTextField2.text = secondTeacherField.text
            self.recButton2.hidden = false
        
        if thirdTeacherField.text == ""
        
            self.recTextField3.hidden = true
            self.recTextField3.text = thirdTeacherField.text
            self.recButton3.hidden = true
        
        else
        
            self.recTextField3.hidden = false
            self.recTextField3.text = thirdTeacherField.text
            self.recButton3.hidden  = false
        

        firstTeacherField.text = self.recTextField1.text
        secondTeacherField.text = self.recTextField2.text
        thirdTeacherField.text = self.recTextField3.text
    

    recAlert.addAction(addAction)
    self.presentViewController(recAlert, animated: true, completion: nil)


@IBAction func editRec1(sender: UIButton)

    if recTextField1.enabled == false
    
        recButton1.setTitle("Save", forState: UIControlState.Normal)
        recTextField1.enabled = true
    
    else
    
        recTextField1.enabled = false
        recButton1.setTitle("Edit", forState: UIControlState.Normal)
        //selectedCollege.location = recTextField1.text!

    


@IBAction func editRec2(sender: UIButton)

    if recTextField2.enabled == false
    
        recButton2.setTitle("Save", forState: UIControlState.Normal)
        recTextField2.enabled = true
    
    else
    
        recTextField2.enabled = false
        recButton2.setTitle("Edit", forState: UIControlState.Normal)
        //selectedCollege.location = recTextField2.text!
    


@IBAction func editRec3(sender: UIButton)

    if recTextField3.enabled == false
    
        recButton3.setTitle("Save", forState: UIControlState.Normal)
        recTextField3.enabled = true
    
    else
    
        recTextField3.enabled = false
        recButton3.setTitle("Edit", forState: UIControlState.Normal)
        //selectedCollege.location = recTextField3.text!
    



func dismissKeyboard()

    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)


@IBAction func editLocationButtonTapped(sender: UIButton)

    if locationEditingTextField.enabled == false
    
        editLocationButtonTapped.setTitle("Save new location", forState: UIControlState.Normal)
        locationEditingTextField.enabled = true
    
    else
    
        locationEditingTextField.enabled = false
        editLocationButtonTapped.setTitle("Edit Location", forState: UIControlState.Normal)
        selectedCollege.location = locationEditingTextField.text!

    


@IBAction func editImage(sender: UIButton)


    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    presentViewController(imagePicker, animated: true, completion: nil)



func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    imagePicker.dismissViewControllerAnimated(true, completion:
        
            let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            self.imageView.image = selectedImage
        
    )



【问题讨论】:

【参考方案1】:

该错误中有几个单独的问题。其中之一是:

CoreData:错误:调用 NSManagedObject 类 'OCP.SecondVCItem' 上的指定初始化程序失败

这是因为您在创建 SecondVCItem 时没有使用指定的初始化程序 (init(entity, insertIntoManagedObjectContext))。查看您的代码,这可能是由于这一行:

self.items = SecondVCItem().fetchAll(self.coreDataDB!)

SecondVCItem() 调用普通初始化程序 (init()) 而不是指定的初始化程序。但我怀疑您实际上希望 fetchAll 成为类方法,而不是实例方法。如果是这样,您将需要使用 class 关键字来定义它:

class func fetchAll(....

然后你可以使用类名调用方法不带括号

self.items = SecondVCItem.fetchAll(self.coreDataDB!)

另一个问题是这样的:

*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'keypath name not found in entity'

这可能是由这行引起的:

navigationItem.title = selectedCollege.name

检查Item的实体和类定义;看起来该实体没有name 属性。

【讨论】:

以上是关于带有核心数据未捕获的 NSExeption 类型异常的 SIGABRT的主要内容,如果未能解决你的问题,请参考以下文章

数据表未捕获类型错误:无法读取未定义的属性“添加”(asp.net / js)

VueJS 3 / 路由器 / 带有推送的重定向:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“推送”)

已修复:Wordpress 站点未捕获类型错误:jQuery(...).live 不是导致带有 JS 的图像不显示的函数

带有扩展的 Swift 核心数据

未捕获的类型错误:无法读取未定义的属性“authenticateClientAndRetrieveSessionId”

核心数据 - 由于未捕获的异常“NSUnknownKeyException”而终止应用程序