Всем привет, новичок на Swift, поэтому прошу помощи. Не могу поместить данные в таблицу из API Что делаю не правильно?
На странице API 20 персонажей, не могу перенести ни 1 данного в таблицу... подскажите пожалуйста Вот код:
class UsersTableViewController: UITableViewController {
var jBonuses = [Results]()
override func viewDidLoad() {
super.viewDidLoad()
Loadchar()
}
func Loadchar() {
let urlString = "https://rickandmortyapi.com/api/character/"
if let url = URL(string: urlString)
{
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, responce, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
self.parseJson(weatherData: safeData)
}
}
task.resume()
self.tableView.reloadData()
}
}
func parseJson(weatherData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(JSONData.self, from: weatherData)
print(decodedData.results[0].name)
} catch {
print(error)
}
}
struct JSONData: Decodable {
let results: [Results]
}
struct Results: Decodable {
let name: String
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return jBonuses.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "user", for: indexPath)
let news = jBonuses[indexPath.row]
cell.textLabel?.text = news.name
return cell
}
}