SIGABRT与coredata未被捕获的类型NSExeption异常

时间:2016-03-04 18:51:38

标签: swift core-data save swift2 uitextfield

我正在制作一个使用核心数据的应用。我正在尝试创建一个名为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 个答案:

答案 0 :(得分: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',原因:'在实体中找不到密钥路径名称'

这可能是由这一行引起的:

navigationItem.title = selectedCollege.name

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