【发布时间】:2018-09-22 04:30:23
【问题描述】:
构建 ToDo 应用。创建新的 todo 任务时应用程序崩溃。
断点停止代码并返回:
线程 1:致命错误:在展开可选值时意外发现 nil
@IBAction func doneButton(_ sender: UIButton) {
guard let title = textView.text, !title.isEmpty else {
return
}
let todo = Todo(context: managedContext)
todo.title = title
todo.priority = Int16(segmentedControl.selectedSegmentIndex)
todo.date = Date()
do {
try managedContext.save()
dismiss(animated: true)
textView.resignFirstResponder()
} catch {
print("Error saving todo: \(error)")
}
}
@IBAction func cancelButton(_ sender: UIButton) {
dismiss(animated: true)
textView.resignFirstResponder()
}
任何想法可能导致应用程序崩溃?谢谢
【问题讨论】:
-
如何声明
textView?还有segmentedControl?如果这些是 IBOutlets,请确保它们在情节提要中正确连接。 -
@LinusGeffarth IBOutlet weak var textView: UITextView!
-
这是初学者常见的错误。在谷歌中搜索你会找到数千个答案
-
它们是否与情节提要正确连接?
标签: ios swift fatal-error