I get the following error message thread 1: EXC_BAD_ACCESS (code=1, address = 0x70
when I am in my addCustomerViewController
and either click save, which at the end of the code runs self.navigationController!.popViewControllerAnimated(true)
, or when I click back in the same addCustomerViewController
. The line of code highlighted with the error is in my AppDelegate class and its the first line as follows
class AppDelegate: UIResponder, UIApplicationDelegate{
This is the last message I get in the debug area as well. Not sure if it is related.
2015-06-17 23:03:03.744 SalesCRM[4930:1409291] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Customer'
Here is the save function. Let me know what other code you might need to see. I am lost as to how to fix this.
@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)
}