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?
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