【问题标题】:Swift and UILabel - fatal error: unexpectedly found nil while unwrapping an Optional valueSwift 和 UILabel - 致命错误:在展开可选值时意外发现 nil
【发布时间】:2015-09-20 17:18:17
【问题描述】:

当我尝试在 Swift 中设置 UILabel 时,应用程序崩溃并且我收到一条错误消息:“致命错误:在展开可选值时意外发现 nil”这是我的代码,当我设置框架时它崩溃了。

var newsFeed1: UILabel!
let Description = object!["Description"] as! String
newsFeed1.frame = CGRect(x: 22.5, y: 315, width: 475, height: 50)
newsFeed1.text = Description
storeTab.addSubview(newsFeed1)

【问题讨论】:

  • 你在哪里给newsFeed1赋值?你错过了newsFeed1 = UILabel()这行!
  • 还要检查object 不是nil
  • @luk2302 谢谢我刚刚注意到我忘记了

标签: ios swift uilabel


【解决方案1】:

你的代码崩溃是因为你试图解开可选的newsFeed1,它是nil——通过使用!,你告诉程序必须有一些不同于nil的东西,而nil没有。因为您从不分配任何东西,并且显然也没有将其作为插座连接。

要解决此问题,您必须将某些内容分配给 newsFeed1 - 在访问它的任何属性之前,您必须输入以下行:

newsFeed1 = UILabel(frame: CGRect(x: 22.5, y: 315, width: 475, height: 50))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多