I think you are updating user's location by making an HTTP request with the latitude and longitude values in the query string or body of the request.
There are couple of options you can try:
Nil coalescing operator
The nil coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.
Assuming we send empty string in the request for any non-existent value:
func updateloc(lat : Double?, long : Double?) {
let latitude = String(lat) ?? ""
let longitude = String(long) ?? ""
let data = "lat=\(latitude)&long=\(longitude)"
...
}
Optional binding
You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.
Assuming we don't trigger the request for any non-existent value:
func updateloc(lat : Double?, long : Double?) {
if let latitude = lat, longitude = long {
let data = "lat=\(latitude)&long=\(longitude)"
}
else {
// Don't make a request..
}
}
By the way it is more readable to pass the location as CLLocationCoordinate2D
type instead of passing latitude and longitude separately:
func updateloc(coordinate: CLLocationCoordinate2D?) {
if let coordinate = coordinate {
let data = "lat=\(coordinate.latitude)&long=\(coordinate.longitude)"
}
else {
// Don't make a request..
}
}