Я создаю приложение, которое делает запрос к API и отображает ответ в виде таблицы.Я использую с помощью декодируемых.Я могу видеть ответ, когда я его печатаю.Но я не могу отобразить его в виде таблицы.Думал, что моя логика была правильной.Как я могу отобразить ответ на табличное представление.Спасибо за все ответы и объяснения.
Модель
struct Businesses: Decodable {
let businesses : [Business]
}
struct categorie : Decodable{
let title: String
}
struct Business: Decodable {
let name: String
let categories: [categorie]
let rating: Double
let price: String?
}
Просмотр контроллера
var businesses = [Business]()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let business = businesses[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:
indexPath) as! DiscoverTableViewCell
cell.restaurantNameLabel.text = business.name
cell.restaurantLocationLabel.text = business.name
cell.typeLabel.text = business.name
return cell
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return businesses.count
}
func network (){
let task = session.dataTask(with: request) { (data, response, error) in
if let response = response as? HTTPURLResponse {
print(response)
} else {
print("error")
}
guard let data = data else {return}
// print(String(data: data, encoding: .utf8) ?? "???")
do {
let businessesJSON = try JSONDecoder().decode(Businesses.self, from: data)
self.tableView.reloadData()
print(businessesJSON)
}
} catch {
print("Error parsing JSON: \(error)")
}
}
task.resume()
}