0

I'm getting values for latitude and longitude from Firebase and store as String into aLatitudeArray and aLongitudeArray. That part works well, arrays are populated as the childs change in Firebase. I want then reconstruct an array of CLLocation2D from the earlier arrays, but when I assign the values to a variable it get nil. My function is :

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 )



        }
    }

I read many posts but no solution suggested worked.

At run values are :

firebaseAlertLatidute String "37.33233141"

stringedLatitude Double 37.332331410000002 ( extra 0000002 added after conversion )

firebaseAlertLongitude String "-122.0312186"

stringeLongitude Double -122.0312186

recombinedCoordinate CLLocationCoordinate2D? nil none ( this is from the error line ).

And from console I get this prints:

fir aLongitudeArray ["-122.0312186"]

fir aLatitudeArray ["37.33233141"]

Why is not assigning the value?

Abizern
  • 129,329
  • 36
  • 198
  • 252
Vincenzo
  • 2,379
  • 12
  • 43

2 Answers2

0

Well, there is no big problem here. You just did wrong using the ! when declaring the recombinedCoordinate variable.

This line declares a variable, and tells Swift: Hey, currently I'm not initializing this, but I'm going to initialize it, believe me.
var recombinedCoordinate: CLLocationCoordinate2D!

But then, on the next line, you are trying to set a variable of this instance.
recombinedCoordinate.latitude = stringedLatitude

See where I am going with this? You have not initialized a CLLocationCoordinate2D instance. recombinedCoordinate is nil. Avoiding nil access is the main reason as to why Swift has the Optional type everywhere.

If you had written CLLocationCoordinate2D? XCode would have told you later, that this call is unsafe, or, it would have not attempted to set the property after seeing that it is nil.


To solve your problem, I'd just write the following:

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

Also, I would advise you to improve your variable naming. "stringedLatitude" and "stringeLongitude" make no sense, because they actually are of the Double type.

Finally, I'd avoid using .doubleValue, see https://stackoverflow.com/a/32850058/3991578

Carsten Hagemann
  • 576
  • 5
  • 20
  • @ Carsten. Yes that was the problem and thanks for the explanation. I didn't know I had to initialise it. I had to fix also the ` while `. condition so to make sure that the arrays count would stay the same or exit. As it was before I get the index out of range error.So far I follow these steps: 1 convert user location or tapped CLLocationCoordinate2D into strings as soon as I get them, 2 post the to firebase , 3 get them back in Xcode. 4 convert them to Doubles ( avoiding .doublevalue ), 5 recombine them into CLLocationCoordinate2D, 6 append them into an array to draw MKAnnotation – Vincenzo Aug 04 '18 at 17:26
  • One thing I could do better is to do the conversion to Doubles directly on the incoming data from Firebase, but than could I just recompact them as a CLLocation2D and append the m to the array? should I start a new question for this or edit this one including the full code? – Vincenzo Aug 04 '18 at 17:26
  • @vincenzo I'd say create another question. I don't fully understand what your problem right now is :) – Carsten Hagemann Aug 04 '18 at 20:15
  • @ Carsten Ok, I created another question @ https://stackoverflow.com/questions/51697627/annotationviewimage-doesnt-get-the-right-image-in-swift thanks again. – Vincenzo Aug 05 '18 at 19:37
0

You need to init it like this

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

As this

var recombinedCoordinate: CLLocationCoordinate2D!
recombinedCoordinate.latitude = stringedLatitude // here recombinedCoordinate is nil as you never initiated it
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57