致命错误:在展开Optional值(lldb)时意外发现nil

时间:2016-03-05 19:57:18

标签: ios arrays swift uitableview

我遇到了致命错误的常见错误:在展开Optional值(lldb)时意外发现nil 。我已经研究了其他关于SO的问题,但这些都没有回答我的问题。

错误出现在以下代码的第4行:

if content[indexPath.row].urgent == true {
        cell.urgentLabel.text = "URGENT"
} else {
        cell.urgentLabel.text = ""
}

关于SO的其他问题说我应该在第4行有"",但我如何解决这个问题,它仍然没有显示任何文字。

此代码对应于以下数组:

var content:[Agenda] = [
    Agenda(subject: "Read this article", deadline: "1-2 days", urgent: false),
    Agenda(subject: "Respond to this email", deadline: "ASAP", urgent: true),
    Agenda(subject: "Add this to diary", deadline: "When at home", urgent: true),
    Agenda(subject: "Listen to this song", deadline: "When finished working", urgent: false),
    Agenda(subject: "Check out this holiday destination", deadline: "At the weekend", urgent: false),
    Agenda(subject: "Download this podcast", deadline: "1-4 days", urgent: false),
    Agenda(subject: "Update notes", deadline: "When at home", urgent: true)
]

1 个答案:

答案 0 :(得分:0)

由于我在此代码中没有看到任何强制解包的选项,因此这个问题不是来自这个常见问题。

尽管如此,这是一个强制解开的零问题,所以......它可能来自隐式解包的可选,例如IBOutlet。

如果InterfaceBuilder和IBOutlet之间的连接丢失,则对象将为nil,并且在尝试访问对象属性时应用程序将崩溃。

解决方案是在InterfaceBuilder中重新创建指向您的案例urgentLabel的IBOutlet /对象的链接。