修复Firebase Tableview数据显示

时间:2019-05-22 08:10:00

标签: ios swift firebase firebase-realtime-database tableview

我正在设置UITableView以在我的App上显示数据库内容。 不幸的是,该信息未显示并且导致应用程序崩溃。

给出的错误是:

  

线程1:致命错误:展开包装时意外发现nil   可选值

enter image description here

    typealias ItemInfo = (Titulo: String, Description: String, Rating: String, photo: String, email: String, location: String, telefone: String, Linkdetalhes: String)

  fileprivate var items: [ItemInfo]! = [( "Titulo","Description","Rating","photo","email","location","telefone", "Linkdetalhes")]


   (UIApplication.shared.delegate as! AppDelegate).fireBaseRef.observe(.value, with: { snapshot in
            let dictRoot = snapshot.value as? [String : AnyObject] ?? [:]
            let dictAliances = dictRoot["Eventos"] as? [String: AnyObject] ?? [:]
            for key in Array(dictAliances.keys) {
                let alianceDic = dictAliances[key]
                self.items.append(( alianceDic!["Title"] as! String, alianceDic!["photo"] as! String, alianceDic!["Description"] as! String, alianceDic!["Rating"] as! String, alianceDic!["email"] as! String, alianceDic!["location"] as! String, alianceDic!["telefone"] as! String, alianceDic!["Linkdetalhes"] as! String))

                self.tableView?.reloadData()
            }
            print(dictAliances)
        })

1 个答案:

答案 0 :(得分:0)

将代码as! String替换为as? String。请参阅以下内容。忽略强制展开。在字典中可能会找到nill值或缺少键值。

self.items.append(( alianceDic!["Title"] as? String, alianceDic!["photo"] as? String, alianceDic!["Description"] as? String, alianceDic!["Rating"] as? String, alianceDic!["email"] as? String, alianceDic!["location"] as? String, alianceDic!["telefone"] as? String, alianceDic!["Linkdetalhes"] as? String))