使用此代码,我可以显示"结果"
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
}
请求:https://api.themoviedb.org/3/movie/now_playing?api_key=8e3967947a95555f9c430e68d070a0e7&language=en-US&page=1 我怎么能得到"页面"和#34;最大"在"日期"? 谢谢!
答案 0 :(得分:0)
var movies: [NSDictionary]?
var dates: NSDictionary?
var page: Int? // var for page
var maximum: String? // var for maximum in dates
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
// This is how you get to page
print("page: \(responseDictionary["page"]!)")
self.page = responseDictionary["page"] as? Int
print("page: \(self.page!)")
// This is how you get to maximum in dates
self.maximum = self.dates?["maximum"] as? String
print("maximum: \(self.maximum!)")
self.tableView.reloadData()
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
})
task.resume()
}
我希望它有所帮助