我正在设置UITableView
以在我的App上显示数据库内容。
不幸的是,该信息未显示并且导致应用程序崩溃。
给出的错误是:
线程1:致命错误:展开包装时意外发现nil 可选值
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)
})
答案 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))