Im not sure why, but my tableView.delegate & tableView.data are returning errors "Reference to member 'delegate' cannot be resolved without a contextual type" "Reference to member 'dataSource' cannot be resolved without a contextual type" I've done everything around my code to see why I am getting these errors but I do not understand. My code is below
import UIKit
@MainActor class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var TableView: UITableView!
public var meals = [Meal]()
override func viewDidLoad() {
super.viewDidLoad()
fetchJSON {
print("APICompleted")
}
tableView.delegate = self //error//
tableView.dataSource = self //error//
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return meals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle , reuseIdentifier: nil)
cell.textLabel?.text = meals[indexPath.row].strMeal.capitalized
return cell
}
func fetchJSON(completed: @escaping () -> ()) {
let jsonUrlString = "https://www.themealdb.com/api/json/v1/1/filter.php?c=Beef"
guard let url = URL(string: jsonUrlString) else {
print("invalid url string lel")
return
}
print("Select a meal to make today!")
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
return
}
do {
let decoder = JSONDecoder()
let mealDetailResponse = try decoder.decode(MealDetailResponse.self, from: data)
for meal in mealDetailResponse.meals {
print("The Meal is \(meal)")
DispatchQueue.main.async {
completed()
}
}
}catch {
print(error)
}
}.resume()
}
}