Swift newbie here, taking an iOS Development class for the first time. I'm tasked w/ making an app that consumes a Movielist API and returns things such as the Movie Poster, Title, and Description. I got to the step where I add the elements for a Movie Poster, Title + Synopsis and am now getting the error shown in the title. My code goes as follows:
//
// MovieViewController.swift
// KS-Flix
//
// Created by Karan on 2/9/22.
//
import UIKit
class MovieViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
var movies = [[String:Any]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request) { (data, response, error) in
// This will run when the network request returns
if let error = error {
print(error.localizedDescription)
} else if let data = data {
let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
self.movies = dataDictionary["results"] as! [[String:Any]]
self.tableView.reloadData()
// TODO: Get the array of movies
// TODO: Store the movies in a property to use elsewhere
// TODO: Reload your table view data
}
}
task.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movies.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
let movie = movies[indexPath.row]
let title = movie["title"] as! String
cell.titleLabel.text = title
return cell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
My code structure looks like this:
Happy to clarify and show what the other files are, apologies in advance for the vague nature of the question - i've been ripping my hair out over this problem and can't figure. out what's wrong :(