sinusGob sinusGob - 2 years ago 147
Swift Question

Unexpected non-void return value in void function, return multiple values

I have a problem with the function that I created, I want to return multiple values but seems like, I keep getting this error


Unexpected non-void return value in void function


func calculateDistance(_ firstLat: Double, _ firstLong: Double, _ secondLat: Double, _ secondLong: Double) -> (Double, Double, String) {

let URL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(firstLat),\(firstLong)&destinations=\(secondLat),\(secondLong)&key=KEYID"
Alamofire.request(URL)
.responseJSON { response in

if let value = response.result.value {
let json = JSON(value)
let distance = json["rows"][0]["elements"][0]["distance"]["value"].double! / 1000 // Double
let price = distance * 1.0 // Double
let duration = json["rows"][0]["elements"][0]["duration"]["text"] // String
return (distance, price, duration) // Keep getting this error on this line
}
}


}


What did I do wrong? I return the correct data type.

Answer Source

First mistake here is that Alamofire.request... is asynchronous. So you can't return a value fetched using that in your function.

The reason you are getting an error is because you are no longer in your function when you are trying to return. You are in the function that is the completion handler of the Alamofire request.

In order to get something out of this you will have to pass it into a completion handler of your own. Like this...

func calculateDistance(_ firstLat: Double, _ firstLong: Double, _ secondLat: Double, _ secondLong: Double, completionHandler: @escaping (Double, Double, String) -> ()) {

    let URL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(firstLat),\(firstLong)&destinations=\(secondLat),\(secondLong)&key=KEYID"
        Alamofire.request(URL).responseJSON { response in
            if let value = response.result.value {
                let json = JSON(value)
                let distance = json["rows"][0]["elements"][0]["distance"]["value"].double! / 1000 // Double
                let price = distance * 1.0 // Double
                let duration = json["rows"][0]["elements"][0]["duration"]["text"] // String
                completionHandler(distance, price, duration) // Keep getting this error on this line
            }
        }
    }
}

The signature (Double, Double, String) -> () is a function that takes in two Doubles and a String. When you get the data back from the network you are then calling this function.

Then you would call it like...

calculateDistance(1, 2, 3) { (distance, price, duration) in
    // do something with the distance price and duration
}

Just another note here... It is really bad practice to squash the parameter names in your function by using _. Do it (maybe) for the first parameter if it is obvious from the function name what it is but don't do it for the following parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download