以下不起作用。以下是应用程序崩溃后它告诉我的内容:
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()
}
}
为什么我在解开可选值时遇到错误?
编辑:这是它说的导致问题的路线: 让newTeacher = TeacherObject(实体:实体!,insertIntoManagedObjectContext:context)
答案 0 :(得分:0)
解包的对象必须是entity!
,因为它是函数调用中唯一未解包的对象。因此,您创建变量entity
的调用最有可能返回nil。
"Teachers"
(看起来很可疑:为什么会复数?),nil
,在这种情况下,您必须返回到应用代理并检查原因。