Reputation: 331
I have written this code (In the function I am using the SwiftSky Api)
// Asked question about WEATHER
public func weatherQuestion(question: String, userLatitude: Double, userLongitude: Double) -> String {
var answer = String()
// Setting up SwiftSky
SwiftSky.secret ="...."
SwiftSky.language = .italian
SwiftSky.locale = .current
SwiftSky.units.temperature = .celsius
SwiftSky.units.accumulation = .centimeter
SwiftSky.units.distance = .kilometer
SwiftSky.units.precipitation = .millimeter
SwiftSky.units.pressure = .millibar
SwiftSky.units.speed = .kilometerPerHour
if question.contains("meteo") {
SwiftSky.get([.current, .alerts, .days, .hours, .minutes], at: Location(latitude: userLatitude, longitude: userLongitude) , on: Date() , { (result) in
switch result {
case .success(let forecast):
answer = (forecast.current?.summary)!
case .failure(let error):
print(error)
}
})
}
return answer
}
The problem is that, when I try to use the result of the function by printing its value (using the code below)
print(weatherQuestion(question: text, userLatitude: Double(self.locationCoordinates.latitude), userLongitude: Double(self.locationCoordinates.longitude)))
it doesn't print anything. How can I use the result of the function so that when I print out the value it is not empty?
Upvotes: 1
Views: 118
Reputation: 10050
SwiftSky.get
works async and calls the passed closure with the result parameter when operation is done. So, weatherQuestion
returns immediately.
Change method signature to:
public func weatherQuestion(question: String, userLatitude: Double, userLongitude: Double, completion: (String) -> Void)
and call it:
case .success(let forecast):
completion(forecast.current!.summary)
Now print in completion:
weatherQuestion(question: text, userLatitude: Double(self.locationCoordinates.latitude), userLongitude: Double(self.locationCoordinates.longitude), completion: { answer in
print(answer)
})
Upvotes: 1