The MovieDBという映画データベースのAPIを使って映画一覧をJSONから抜き出したいのですが、moviesにJSONから抜き出したresultをinitで設定しているのですが、いざMovieListを使ってMovieList.moviesを表示しようとすると空の配列になってしまいます。
なぜ空に戻ってしまうのでしょうか?途中のprint(weakSelf.movies[0]["title"]!)ではデータを表示できています。
API_KEYを公開できないため実行はできないと思います。
import Foundation
import UIKit
class MovieList{
let apiKey: String = "API_KEY"
var pageURL: String
var movies : [[String: Any]] = []
init(page: Int){
pageURL = "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)&language=ja&page=\(page.description))"
guard let url: URL = URL(string: pageURL) else {
print("URL not constructed")
return
}
let task: URLSessionTask = URLSession.shared.dataTask(with: url) { [weak self] (data, res, err) in
do {
if let error = err {
throw error
}
guard let jsonData = data else {
print("data is nil")
return
}
guard let obj = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] else {
print("JSON Serialization failed")
return
}
guard let results = obj["results"] as? [[String: Any]] else {
print("items is not much type")
return
}
DispatchQueue.main.async { [weak self] in
guard let weakSelf = self else {
print("self is already deallocated")
return
}
weakSelf.movies = results
print(weakSelf.movies[0]["title"]!)
}
}catch {
print(error)
}
}
task.resume()
}
}
var movieList: MovieList = MovieList(page: 1)
print(movieList.movies)
{
"results": [
{
"popularity": 138.944,
"vote_count": 255,
"video": false,
"poster_path": "\/9zrbgYyFvwH8sy5mv9eT25xsAzL.jpg",
"id": 531454,
"adult": false,
"backdrop_path": "\/jMO1icztaUUEUApdAQx0cZOt7b8.jpg",
"original_language": "en",
"original_title": "Eurovision Song Contest: The Story of Fire Saga",
"genre_ids": [
35,
10402
],
"title": "ユーロビジョン歌合戦 〜ファイア・サーガ物語〜",
"vote_average": 6.6,
"overview": "世界最大級の歌合戦に出場するチャンスを手にした小さな町のデュオ。だが有力なライバルや邪魔者、舞台での思わぬ出来事が、夢をかなえたい2人の行く手を阻む。",
"release_date": "2020-06-26"
},
{
"popularity": 107.457,
"id": 475430,
"video": false,
"vote_count": 548,
"vote_average": 5.9,
"title": "アルテミスと妖精の身代金",
"release_date": "2020-06-12",
"original_language": "en",
"original_title": "Artemis Fowl",
"genre_ids": [
12,
14,
878,
10751
],
"backdrop_path": "\/o0F8xAt8YuEm5mEZviX5pEFC12y.jpg",
"adult": false,
"overview": "",
"poster_path": "\/mhDdx7o7hhrxrikq8aqPLLnS9w8.jpg"
},
{
"popularity": 84.03,
"id": 619592,
"video": false,
"vote_count": 46,
"vote_average": 5.3,
"title": "Force of Nature",
"release_date": "2020-07-02",
"original_language": "en",
"original_title": "Force of Nature",
"genre_ids": [
28,
18
],
"backdrop_path": "\/jAtO4ci8Tr5jDmg33XF3OZ8VPah.jpg",
"adult": false,
"overview": "",
"poster_path": "\/ucktgbaMSaETUDLUBp1ubGD6aNj.jpg"
}
]
}