【问题标题】:"Fatal error" crash at "if let"“如果让”时出现“致命错误”崩溃
【发布时间】:2017-02-12 15:23:50
【问题描述】:

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

我无法在我工作的设备或模拟器上重现它。当有人使用 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
    }

【问题讨论】:

  • 你能提供更多代码吗?
  • @Asdrubal 我编辑了我的问题
  • 如果 l.date != nil 则尝试
  • ls 是如何声明的?
  • @TimVermeulen 在类大括号中:var ls: [LObject] = [] 然后在 viewDidLoad 我从 Core Data 获取列表。

标签: ios swift xcode crash fatal-error


【解决方案1】:

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

尝试查看第 3 行的代码:

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

并查看编译器是否因为警告或是否是您所做的事情而使您执行此转换为 LTableViewCell。通常它可能会构建,但可能会导致崩溃,因为它不是匹配的数据类型。

【讨论】:

  • “看起来好像您正试图在崩溃的行上的一个数组内部进行转换” 您这是什么意思?投射什么并将其投射到什么数组中?
  • “看看编译器是否让你做这个转换为 LTableViewCell 是因为一个警告,或者这是你做的事情。”让我怎么做?
  • this ls[(indexPath as NSIndexPath).row] - ls 是一个索引数组 (indexPath as NSIndexPath).row 以这种方式进行数组索引有点不合常规,但如果它有效的话。此外,在您声明 l 对象的行上,演员表也将 as! 转换为 LTAbleViewCell。它崩溃的 if 语句在逻辑上是正确的。因此,当您创建要比较的变量时,崩溃就发生了。
  • 那或者当您使用l.date 时,没有.date 可供使用。这是您使用 Date date = new Date() 之类的东西创建的日期对象,还是 DateFormatter 库的一部分?
  • 我还是不明白你的意思。演员阵容有什么问题?为什么崩溃出现在 if 语句在逻辑上是正确的?
【解决方案2】:

我似乎已经解决了这个问题。当用户的手机将日期和时间设置为以 12 小时格式显示时,就会发生这种情况。我阅读了this answer 并添加了以下行:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多