I am trying to get a function that calls an API via HTTP request and return a String. My request function has a completion handler that returns the Data and it is used by my function.
func buildHTML () -> String {
var htmlString: String = ""
request(url: "https://someurl.com/api/getalldata") { (jsonData: Data) in
let units = try JSONDecoder().decode([MyUnits].self, from: jsonData)
for unit in units {
//Building my html string here
htmlString += "......."
}
}
return htmlString
}
However this does not work, because the htmlString returned as an empty string, How can I get this string out to return it by buildHTML?
Any idea is welcomed!