我想比较两个单独的服务器调用的结果,并且在如何使用闭包格式进行操作方面感到困惑。
例如,如果一个天气api一次返回一个城市的温度,并且我想比较两个城市的温度,那么我就在如何等待两个调用完成方面苦苦挣扎。
我正在使用的,从单个服务器调用改编的代码可以等待第一个结果。但是,我不知道该如何等待第二个结果。
这是我的代码:
func compareWeather (onecity:String, anothercity:String,completion:@escaping (_ response:String)->()){
let city1:String = "Boston"
let city2:Strng = "Palo Alto"
self.getWeatherForCity(city: city1){//open 2
answer in
let temp1:String = answer
}
self.getWeatherForCity(city: city2){//open 2
answer in
let temp2:String = answer
}
//THIS DOES NOT WORK Since it is called before we here back from APIS BUT THIS IS WHAT I WANT TO accomplish
completion("the temperature in Boston is ",temp1, "but the temperature in Palo Alto is ",temp2)
}