let url = URL(string: "https://api.letsbuildthatapp.com/jsondecodable/courses")
URLSession.shared.dataTask(with: url!) { (data, response, err) in
guard let data = data else { return }
do {
let appData = try JSONDecoder().decode([App].self, from: data)
print(appData)
} catch let err {
print("Err", err)
}
}.resume()
This is how I parsed the json. But I want to add those parsed data to my array to use it to populate mytableview.
struct App: Decodable {
var id: Int
var name: String
}
var apps = [App]()
This is what I get after printed the appData in my console.
[CustomTableView.App(id: 1, name: "Instagram Firebase"), CustomTableView.App(id: 2, name: "Podcasts Course"), CustomTableView.App(id: 3, name: "Intermediate Training Core Data"), CustomTableView.App(id: 4, name: "Kindle Basic Training")]
Can you help me to understand this concept because I'm new in iOS and searched a lot to find the answer. Thank you so much
This is my whole code
apps.append(contentOf: appData)
just after theprint(appData)
? But you might want to do atableViewreloadData()
also, and do that in Main Thread.