Я должен разобрать и найти элегантный способ работы с моими проанализированными данными, а также использовать его для правильного заполнения UIElements, таких как UITableViews, UICollectionViews и т. Д. Я опубликую ниже, как я анализирую, а также файл JSON.
import Foundation
struct Contents : Decodable {
let data : [Content]
}
struct Content : Decodable {
let id : Int
let descricao : String
let urlImagem : String
}
Файл ответа API:
import Foundation
var audioBook = [Content]()
func getAudiobooksAPI() {
let url = URL(string: "https://alodjinha.herokuapp.com/categoria")
let session = URLSession.shared
guard let unwrappedURL = url else { print("Error unwrapping URL"); return }
let dataTask = session.dataTask(with: unwrappedURL) { (data, response, error) in
guard let unwrappedDAta = data else { print("Error unwrapping data"); return }
do {
let posts2 = try JSONDecoder().decode(Contents.self, from: unwrappedDAta)
audioBook = posts2.data
} catch {
print("Could not get API data. \(error), \(error.localizedDescription)")
}
}
dataTask.resume()
}
Файл TableView:
Как заполнить данные, которые я проанализировал?Мне действительно сложно это делать.Мне нужно объяснение, которое поможет мне сделать это, используя элегантный способ, а также в качестве старшего разработчика.
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
getAudiobooksAPI()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ?????
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell
???????
return cell
}
}
JSON:
Модель JSON