首页 文章

Swift和UILabel - 致命错误:在展开Optional值时意外发现nil

提问于
浏览
0

当我尝试在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)

1 回答

  • 0

    你的代码崩溃了,因为你试图打开可选的 newsFeed1 这是零 - 通过使用 ! 你告诉程序有不同于nil的东西,没有 . 因为你从来没有分配任何东西,并且显然没有将它作为插座连接 .

    要解决此问题,您需要为 newsFeed1 指定一些内容 - 在访问它的任何属性之前,您必须输入以下行:

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

相关问题