【问题标题】:Unexpectedly found nil while unwrapping an Optional value / Swift在展开可选值/Swift 时意外发现 nil
【发布时间】: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() 
}

任何想法可能导致应用程序崩溃?谢谢

【问题讨论】:

标签: ios swift fatal-error


【解决方案1】:

UISegmentedControlSegment 是公共枚举,UISegmentedControlUIControl 根据您的评论,您似乎将 UISegmentedControl 误认为是 UISegmentedControlSegment,因此连接 UISegmentedControl 如下:

@IBOutlet weak var segmentedControl: UISegmentedControl!

【讨论】:

  • 谢谢!但是我自己已经修好了,应用还是不行
  • 好收获! @曼西
猜你喜欢
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多