Problem with returning an optional type Double from a JSON data stream

I am trying to decode a JSON data stream that contains the latitude and longitude of a given location on Earth…

Here is the geocoding data structure…

// ================================================================================
/// Under "results" there are a number of elements including "place_id", however we are only interested in the latitude and longitude that is found under "geometry" Element
struct resultsData : Decodable
    var geometry: geometryLocation

}// close struct geometryResults : Decodable

// ================================================================================
/// under the "geometry" Element is the "Location" Element that contains the latitude and longitude

struct geometryLocation :Decodable

    var location :  locationLatLng

}// close struct geometryLocation :Decodable

// ================================================================================
/// Under the "Location" Element is the items that we wish to return, the latitude and longitude for
/// the string address.
struct locationLatLng :Decodable

    var lat: Double
    var lng: Double

}// close struct locationLatLng :Decodable

// ================================================================================

And here is part of my code to pull out the lat [latitude]… but I am getting an error I cannot resolve…

import Foundation
import SwiftUI
import Combine

// ===================================================================================
class AddressDecodeLatLng:ObservableObject
    // Properties
    @ObservedObject var geocodedaddress = addressGeoDecoding()
    /// this is creating a new Observed object instance of the class "addressGeocodeLatLng()"
    var gcdedResultsData: geocodeResultsData? = nil
    ///initialise the variables representing the latitude and longitude
    @State var latitud:  Double? = 0.00
    @State var longitud: Double? = 0.00
    // Methods
    func retrieveGecondingLatLngData(addressUncoded : String )
        geocodedaddress.fetchGecondingLatLngData(addressUncoded: addressUncoded)
    }// close func retrieveGecondingLatLngData(addressUncoded : addressUncoded )
    /// This method decodes the latitude information for from the JSON data returned from Google address geocoded data
    func retrieveLatitude ( ) -> Double
        let locationResultsArray = geocodedaddress.gcodedResultsData?.results[0]
        let locationGeometry = locationResultsArray?.geometry
        let locationElement = locationGeometry?.location
        if (locationElement != nil)
                 latitud = locationElement?.lat
                 return latitud
                // get an error message Value of optional type 'Double?' must be unwrapped to a value of type 'Double'

         }// close if let locationLocation = locationGeometry?.location
            print("Geocoding data for Latitude not available")

         }// close if let locationLocation = locationGeometry?.location
    }// close retrieveLatitude ( )

I am getting the following error message from the compiler…
" Value of optional type ‘Double?’ must be unwrapped to a value of type ‘Double’"

I thought by wrapping “latitud = locationElement?.lat” in an if … else block I could resolve any issues with returning the value of “latiud”… and it was also defined in my code as “Double”…

Any suggestion how I can resolve this?
Thank you in advance…

I think this will work:

latitud = ?? 0.0

That should set latitud to zero if locationElement is nil,
But since you check for nil in the ‘if’ statement, you can use

latitud = locationElement!.lat

Even better,

if let locationElement

If your JSON data includes items (properties) that may or may not have data then that property in the structure you are loading into should be defined as an Optional.

When you then go to use that data you should unwrap it by using an if let… construct as Peter is describing above. That way you can access the data safely.