Mit diesem Code i das "Ergebnis" zeigenLesen JSON-Datei mit Swift 3
var movies: [NSDictionary]?
var dates: NSDictionary?
func fetchNowPlayingMovies(){
let apiKey = "8e3967947a95555f9c430e68d070a0e7"
let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)"+"&language=en-US&page=1")
let request = URLRequest(url: url! as URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10)
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main)
let task: URLSessionDataTask = session.dataTask(with:request, completionHandler: {(dataOrNil, reponse, error) in
if let data = dataOrNil{
if let responseDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
//print("reponse: \(responseDictionary)")
self.movies = responseDictionary["results"] as? [NSDictionary]
self.dates = responseDictionary["dates"] as? NSDictionary
self.tableView.reloadData()
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
})
task.resume()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieTableViewCell
//MARK: - JSON Properties
let movie = movies![indexPath.row]
let date = dates![indexPath.row]
let title = movie["title"] as! String
let vote_average = movie["vote_average"] as! Double
let overview = movie["overview"] as! String
let baseUrl = "http://image.tmdb.org/t/p/w500"
let posterPath = movie["poster_path"] as! String
let imageUrl = URL(string: baseUrl + posterPath)
// Configure the cell...
cell.lblTitle.text = title
cell.lblOverview.text = overview
cell.lblVoteAVG.text = String(vote_average)
cell.imgPosterPath.sd_setImage(with: imageUrl)
return cell
}
Anfrage: https://api.themoviedb.org/3/movie/now_playing?api_key=8e3967947a95555f9c430e68d070a0e7&language=en-US&page=1 , wie ich die "Seite" erhalten und das "Maximum" in "Termine"? Vielen Dank!
half nicht Screenshot von Code schreiben Sie. Real Code kopieren/einfügen. Vermeiden Sie 'NSDictionary' in Swift3, bevorzugt Swifty Types Dictionary. Seite ist auf der gleichen Ebene wie Ergebnisse, also responseDictionary ["Seite"]? Gleiches gilt für die Daten. – Larme
Ich bin ein Neuling, also verstehe ich nicht sehr gut. Kannst du das mit Code erklären? Vielen Dank! – NAK