gpt4 book ai didi

ios - 展开可选值/Swift 时意外发现 nil

转载 作者:行者123 更新时间:2023-11-28 14:52:00 24 4
gpt4 key购买 nike

构建 ToDo 应用程序。创建新的待办事项任务时应用程序崩溃。

断点停止代码并返回:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

 @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()
}

有什么可能导致应用程序崩溃的想法吗?谢谢

最佳答案

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

@IBOutlet weak var segmentedControl: UISegmentedControl!

关于ios - 展开可选值/Swift 时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com