"Fatal error" 在 "if let" 崩溃

"Fatal error" crash at "if let"

我遇到了一个崩溃,除了致命错误外没有其他信息。我可以在 Xcode 中的 'Organiser' 中看到它,它指向以下行:

我无法在我工作的设备或模拟器上重现它。当有人使用 TestFlight 的测试版时,就会发生这种情况。

#0  (null) in specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never ()
#1  (null) in specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never ()
#2  (null) in ...TableViewController.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell at ...TableViewController.swift:103

更多代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "LCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LTableViewCell        
    let l = ls[(indexPath as NSIndexPath).row]
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = NSLocalizedString("dd/MM/yyyy", comment: "")

    if let date = l.date{            
        let dateString = dateFormatter.string(from: date)
        cell.dateLabel.text = dateString
    }

考虑阅读 here or here 以更好地了解崩溃报告的来源。看起来好像您正在尝试在创建崩溃对象的行上的数组内部进行强制转换。但通常崩溃的根源在此之前......

尝试查看第 3 行的代码:

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LTableViewCell

并查看编译器是否因为警告而让您将此强制转换为 LTableViewCell,或者您是否确实这样做了。通常它可能会构建,但可能会导致崩溃,因为它不是匹配的数据类型。

我好像解决了问题。当用户的 phone 将其日期和时间设置为以 12 小时格式显示时,就会发生这种情况。我阅读了 并添加了以下行:

    dateFormatter.locale = Locale(localeIdentifier: "en_GB")

崩溃不再发生。我希望崩溃日志不是那么无用。它会节省我很多时间。