线程1:EXC_BAD_ACCESS(代码= 1,appDelegate上的地址= 0x70错误,单击保存或单击返回

时间:2015-06-18 04:05:38

标签: ios swift core-data appdelegate

当我在thread 1: EXC_BAD_ACCESS (code=1, address = 0x70时,我收到以下错误消息addCustomerViewController,然后单击“保存”,代码末尾运行self.navigationController!.popViewControllerAnimated(true),或者单击“相同addCustomerViewController。用错误突出显示的代码行在我的AppDelegate类中,第一行如下

class AppDelegate: UIResponder, UIApplicationDelegate{

这是我在调试区域中收到的最后一条消息。不确定它是否相关。

2015-06-17 23:03:03.744 SalesCRM[4930:1409291] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Customer' 

这是保存功能。让我知道您可能需要查看的其他代码。我迷失了如何解决这个问题。

@IBAction func save(sender: AnyObject) {

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let context:NSManagedObjectContext = appDel.managedObjectContext


    if firstName.text != "" && lastName.text != "" && phoneNumber.text != ""
    {
        let newUser = NSEntityDescription.insertNewObjectForEntityForName("Customer", inManagedObjectContext: context) as NSManagedObject
        newUser.setValue(firstName.text?.lowercaseString, forKey: "firstName")
        newUser.setValue(lastName.text?.lowercaseString, forKey: "lastName")
        newUser.setValue(phoneNumber.text, forKey: "phoneNumber")
        newUser.setValue(email.text, forKey: "email")
        newUser.setValue(notes.text, forKey: "notes")
        var name = String()
        name = lastName.text!
        print(name)
        var index = advance(name.startIndex, 0)
        print(index)
        first = String(name[index]).lowercaseString
        newUser.setValue(first, forKey: "first")

        let todaysDate:NSDate = NSDate()
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy hh:mm"
        let DateInFormat:String = dateFormatter.stringFromDate(todaysDate) + ": created"
        newUser.setValue(DateInFormat, forKey: "log")          
        try! context.save()
        let request = NSFetchRequest(entityName: "Customer")
        let results = try! context.executeFetchRequest(request)
        print(results)

    }
    else
    {

        let alert = UIAlertController(title: "Error", message: "Please enter all information", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    self.navigationController!.popViewControllerAnimated(true)
}

0 个答案:

没有答案