【问题标题】:CLLocation2D doesn't get value assigned from a variableCLLocation2D 没有从变量中获得赋值
【发布时间】:2018-08-04 14:59:01
【问题描述】:

我从 Firebase 获取纬度和经度值,并将其作为字符串存储到 aLatitudeArray 和 aLongitudeArray 中。这部分效果很好,随着 Firebase 中子项的更改,数组会被填充。然后我想从早期的数组中重建一个 CLLocation2D 数组,但是当我将值分配给一个变量时,它得到 nil。我的功能是:

func drawAlerts() {   // to rewrite based on aLatituteArray and aLongitudeArray generated from firebase incoming data
        var alertDrawArrayPosition = 0
        while alertDrawArrayPosition != (alertNotificationArray.count - 1) {

            var firebaseAlertLatidute = aLatitudeArray[alertDrawArrayPosition]  // get String from alertLaitudeArray
            let stringedLatitude: Double = (firebaseAlertLatidute as NSString).doubleValue // converts it to Double 


            var firebaseAlertLongitude = aLongitudeArray[alertDrawArrayPosition]  // get string from alertLongitudeAray
            let stringeLongitude: Double = (firebaseAlertLongitude as NSString).doubleValue //converts it to Double

            var recombinedCoordinate: CLLocationCoordinate2D!
//
            recombinedCoordinate.latitude = stringedLatitude  // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
            recombinedCoordinate.longitude = stringeLongitude  // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

//            alertNotificationArray.append(recombinedCoordinate!) // Build alertNotificationArray



            alertDrawArrayPosition = ( alertDrawArrayPosition + 1 )



        }
    }

我阅读了很多帖子,但没有建议的解决方案有效。

运行时的值为:

firebaseAlertLatidute 字符串“37.33233141”

stringedLatitude Double 37.332331410000002(转换后额外增加0000002)

firebaseAlertLongitude 字符串“-122.0312186”

stringeLongitude Double -122.0312186

recombinedCoordinate CLLocationCoordinate2D?无(这是来自错误行)。

从控制台我得到这个打印:

fir aLongitudeArray ["-122.0312186"]

fir aLatitudeArray ["37.33233141"]

为什么不赋值?

【问题讨论】:

    标签: ios arrays swift cllocation


    【解决方案1】:

    嗯,这里没有什么大问题。你在声明recombinedCoordinate 变量时使用! 做错了。

    这一行声明了一个变量,并告诉 Swift:嘿,目前我没有初始化它,但我要初始化它,相信我。
    var recombinedCoordinate: CLLocationCoordinate2D!

    但是,在下一行,您尝试设置此实例的变量。
    recombinedCoordinate.latitude = stringedLatitude

    看看我的目标是什么?您尚未初始化 CLLocationCoordinate2D 实例。 recombinedCoordinate 为零。避免 nil 访问是 Swift 到处都有 Optional 类型的主要原因。

    如果你写了CLLocationCoordinate2D?,XCode 稍后会告诉你,这个调用是不安全的,或者,它不会在看到它为 nil 后尝试设置该属性。


    为了解决您的问题,我只需写以下内容:

    let recombinedCoordinate: CLLocationCoordinate2D(latitude: stringedLatitude, longitude: stringeLongitude)
    

    另外,我建议您改进变量命名。 "stringedLatitude" 和 "stringeLongitude" 没有意义,因为它们实际上是 Double 类型。

    最后,我会避免使用.doubleValue,请参阅https://stackoverflow.com/a/32850058/3991578

    【讨论】:

    • @卡斯滕。是的,这就是问题所在,感谢您的解释。我不知道我必须初始化它。我还必须修复`while`。条件,以确保数组计数保持不变或退出。就像在我得到索引超出范围错误之前一样。到目前为止,我遵循以下步骤:1 将用户位置或轻敲 CLLocationCoordinate2D 转换为字符串,2 将它们发布到 firebase ,3 将它们放回 Xcode。 4 将它们转换为 Doubles(避免 .doublevalue),5 将它们重新组合成 CLLocationCoordinate2D,6 将它们附加到数组中以绘制 MKAnnotation
    • 我可以做的更好的一件事是直接在来自 Firebase 的传入数据上转换为 Doubles,但我可以将它们重新压缩为 CLLocation2D 并将 m 附加到数组中吗?我应该为此开始一个新问题还是编辑这个包括完整代码的问题?
    • @vincenzo 我想说创建另一个问题。我不完全明白你现在的问题是什么:)
    • @Carsten 好的,我创建了另一个问题@stackoverflow.com/questions/51697627/… 再次感谢。
    【解决方案2】:

    你需要像这样初始化它

    let recombinedCoordinate = CLLocationCoordinate2D(latitude:stringedLatitude, longitude:stringeLongitude)
    

    这样

    var recombinedCoordinate: CLLocationCoordinate2D!
    recombinedCoordinate.latitude = stringedLatitude // here recombinedCoordinate is nil as you never initiated it
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-15
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2017-02-27
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多