The following does not work. Here is what it tells me after the app crashes:
fatal error: unexpectedly found nil while unwrapping an Optional value
class TeacherInputViewController: UIViewController {
@IBOutlet var textName: UITextField!
@IBOutlet var textEmail: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnSave(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let entity = NSEntityDescription.entityForName("Teachers", inManagedObjectContext: context)
let newTeacher = TeacherObject(entity: entity!, insertIntoManagedObjectContext: context)
newTeacher.name = textName.text
newTeacher.email = textEmail.text
context.save(nil)
println(newTeacher)
self.navigationController?.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Why am I getting an error unwrapping an optional values?
EDIT: this is the line it says is causing the problem: let newTeacher = TeacherObject(entity: entity!, insertIntoManagedObjectContext: context)