【问题标题】:Issue with retrieving Double number from Firebase Swift从 Firebase Swift 检索双数的问题
【发布时间】:2018-03-25 05:08:07
【问题描述】:

我目前正在从事餐厅项目,通过按下餐厅视图控制器上的按钮,我正在谷歌地图(地图视图控制器)上查看餐厅的位置。为了在我的地图代码中放置纬度和经度,我在从 Firebase 检索 Double 数字时遇到了问题。每当我尝试打印它们时,我都会收到此错误“致命错误:在展开可选值 (lldb) 时意外发现 nil”

GMSServices.provideAPIKey("AIzaSyRQKeLyKH5CsrDPpPsjy7nYqudEbqXmQS2")
        let ref = Database.database().reference().child("amsterdam")
        ref.queryOrdered(byChild: "name").queryEqual(toValue: self.appDelegate.selectedBarName).observe(.value) { (snapshot) in
            for child in snapshot.children{
                let snap = child as! DataSnapshot
                let mapSnap = snap.childSnapshot(forPath: "map")
                if let snapDict = mapSnap.value as? [String:AnyObject]{
                    let dictValues = [AnyObject](snapDict.values)
                    for each in dictValues{
                        let latitude = each["map_latitude"] as! Double
                        let longitude = each["map_longitude"] as! Double

                        print(latitude)
                        print(longitude)
                       let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 6.0)
                        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
                        self.view = mapView
                        let marker = GMSMarker()
                        marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                        marker.map = mapView
                    }
            }
        }
    }
}

【问题讨论】:

    标签: ios swift google-maps firebase double


    【解决方案1】:

    我认为问题在于从每个中获取价值。 如果 each 作为字典对象,请尝试使用此行获取值。

    let latitude = each.object(forKey:"map_latitude") as! Double
    

    【讨论】:

    • 不幸的是它并没有真正起作用(我收到此消息的此错误:由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFNumber objectForKey:]:无法识别的选择器已发送到实例 0x604000427420'
    • 请检查 each 对象的值以及 dictValues。如果您请提供值 dictValues,那么我将尝试提供解决方案。
    • 我检查了 each 和 dictValues 的值。一切都正确显示。可能问题在于获取纬度和经度的单独值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    相关资源
    最近更新 更多