I am new to swift, and since I work as a backend developer, I thought I would try to make a app, to understand how the data I return from the api I make, is handled. I also do front end sometimes, but it is on the website :D
In c# I know of async/await which can make the code wait to get a result, before continuing. I have tried with completetion handlers, and group.enter(), group.leave(), which didn't work, or I didn't do it right. I know asynce/await isnt a thing yet, in swift, or so did I read.
Here is my code, I have right now:
struct vuItem:Decodable {
var date:String
var temperatureC:Int
var temperatureF:Int
var summary:String
}
var vuItems:[vuItem] = [vuItem]()
func getVejr() -> [vuItem] {
let url = URL(string: "http://192.168.137.1:90/weatherforecast")
var result = [vuItem]()
let session = URLSession.shared
let task = session.dataTask(with: url!, completionHandler: { data, response, error in
// Check the response
print(error)
print(response)
do {
result = try! JSONDecoder().decode([vuItem].self, from: data!)
print(result[0].date)
}
})
task.resume()
return result
}
vuItems = getVejr()
vuItems.forEach { (item) in
print(item.date)
}