每次我运行这个我都会得到这个错误: fatal error :
Unexpectedly found nil while unwrapping an Optional value 2017-09-15 06:30:04.650075+0200 RollerBank[845:211470] fatal error: unexpectedly found nil while unwrapping an Optional value.
我不知道我做错了什么。有人能帮助我吗?
func reload(){
//get data
Database.database().reference().child("Rollerbanken").observe(.value, with: { (snapshot) in
for item in snapshot.children{
if let itemDict = snapshot.value as? NSDictionary {
let annotation = MKPointAnnotation()
annotation.title = itemDict["TypeControle"] as! String
let tijd = itemDict["Tijd"] as! String
annotation.subtitle = "Geplaatst om \(tijd)"
let getLatitude = itemDict["Latitude"] as! Double
let getLongitude = itemDict["Longitude"] as! Double
annotation.coordinate = CLLocationCoordinate2D(latitude: getLatitude, longitude: getLongitude)
self.map.addAnnotation(annotation)
}
}
})
}
最佳答案
使用它来解析从 Firebase 收到的值。
if let value = snapshot.value as? Dictionary<String, Any> {
for key in value.keys {
if let itemDict = value[key] as? Dictionary<String, AnyObject> {
let annotation = MKPointAnnotation()
annotation.title = itemDict["TypeControle"] as! String
let tijd = itemDict["Tijd"] as! String
annotation.subtitle = "Geplaatst om \(tijd)"
let getLatitude = itemDict["Latitude"] as? String
let getLongitude = itemDict["Longitude"] as? String
if let lat = getLatitude, let long = getLongitude {
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long))
self.map.addAnnotation(annotation)
}
}
}
}
关于swift - fatal error Firebase 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231773/