Hello everyone! I've been scouring the internet and yes I have read the many threads on here with this exact scenario and all of the wonderful solutions. However none have worked for my particular application. Here is the API I'm using. API
I've taken the array brackets off of var mealList = [TopMeal]in the viewController about 100 times like most of the internet suggests. I've rearranged the structs several times. I've watched no telling how many YouTube videos and I've texted two people I know.... no solution yet. Just know reposting a duplicate topic is my last resort, I just want to know how to fix it so I can move on. This project is due tomorrow and it's not looking hot. Any help is very appreciated.
Here are my structs:
//Struct for top level meals
struct TopMeal: Codable {
var topMeal: Meals
}
//struct for base meal details
struct Meals: Codable {
var strMeal: String?
var strMealThumb: String?
var idMeal: String?
}```
**Last but not least**
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mealList = [TopMeal]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getApiData {
print("data loaded")
self.tableView.reloadData()
}
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mealList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = "\(mealList[indexPath.row])"
return cell
}
// MealApi.shared.getApiData(onCompletion: anonFunction)
func getApiData(completed: @escaping () -> ()) {
let url = URL(string: "https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
self.mealList = try JSONDecoder().decode([TopMeal].self, from: data!)
DispatchQueue.main.async {
completed()
}
}
catch {
print(error)
}
}
}.resume()
}
}```