swift - fatal error Firebase 快照

标签 swift xcode firebase firebase-realtime-database

每次我运行这个我都会得到这个错误: 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)

                    }
                }
            })
        }

Structure

最佳答案

使用它来解析从 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/

相关文章:

string - Swift nil 可选字符串 = 空字符串

swift - 是否可以将类的属性作为参数传递? ( swift )

ios - UITableViewCell contentView insets 和自动布局

xcode - 处于 Xcode 断点时没有键盘响应 (Mac)

ruby-on-rails - 帮助在 Lion GM 上安装 Ruby 1.9.2

ios - Xcode 8 似乎错误地使用 Swift 3 更改了嵌入式框架函数签名

java - 我可以在简单的 java CRUD 操作中使用 Firebase 吗?

firebase - 是否可以使用 firebase 或 gcp CLI 将我的项目从 Spark 升级到 blaze?

ios - 重新启动并在 GameScene 中呈现

javascript - 从集合中获取第一个元素并将其删除