やりたいこと
JSONのレスポンスをStructにCodableを使いまとめてTableViewにString型として表示したい。
疑問点&エラー
var movies = MovieStruct() Missing argument for parameter 'from' in call(insert 'from' <#Decoder#>)
と表示されます。元々は
var movies = [MovieStruct]()
上記のように配列に入れていたのですがAPIのRequestしたところ配列(辞書型?)にはなっておらず全て{}で返ってきていたため
上手く表示されておらずdecodeも配列で返すのを直したのですがmoviesに構造体からきたデータを格納したいのですが
記述の仕方があまりよく分からず。調べても配列で返ってきているAPIの物ばかりなので質問しました。
ソースコード
import UIKit
class ViewController: UIViewController {
//https://api.themoviedb.org/3/movie/550?api_key=
@IBOutlet weak var movieTableView: UITableView!
var movies = MovieStruct()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fechData()
}
func fechData(){
let url = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=")!
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print(error?.localizedDescription ?? "Unknown error")
return
}
let decoder = JSONDecoder()
if let movies = try? decoder.decode(MovieStruct.self, from: data){
DispatchQueue.main.async {
// self.movies = movies
self.movies.append(movies)
self.movieTableView.reloadData()
}
}else{
print("Unable parse JSON response")
}
}.resume()
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movies.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let movie = movies[indexPath.row]
cell.textLabel?.text = movie.title
cell.detailTextLabel?.text = movie.release_date
return cell
}
}
struct MovieStruct: Codable {
var title: String
var release_date:String
}
レスポンス
{
adult = 0;
"backdrop_path" = "/8iVyhmjzUbvAGppkdCZPiyEHSoF.jpg";
"belongs_to_collection" = "<null>";
budget = 63000000;
genres = (
{
id = 18;
name = Drama;
}
);
homepage = "http://www.foxmovies.com/movies/fight-club";
id = 550;
"imdb_id" = tt0137523;
"original_language" = en;
"original_title" = "Fight Club";
overview = "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.";
popularity = "39.218";
"poster_path" = "/wR5HZWdVpcXx9sevV1bQi7rP4op.jpg";
"production_companies" = (
{
id = 508;
"logo_path" = "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png";
name = "Regency Enterprises";
"origin_country" = US;
},
{
id = 711;
"logo_path" = "/tEiIH5QesdheJmDAqQwvtN60727.png";
name = "Fox 2000 Pictures";
"origin_country" = US;
},
{
id = 20555;
"logo_path" = "/hD8yEGUBlHOcfHYbujp71vD8gZp.png";
name = "Taurus Film";
"origin_country" = DE;
},
{
id = 54051;
"logo_path" = "<null>";
name = "Atman Entertainment";
"origin_country" = "";
},
{
id = 54052;
"logo_path" = "<null>";
name = "Knickerbocker Films";
"origin_country" = US;
},
{
id = 25;
"logo_path" = "/qZCc1lty5FzX30aOCVRBLzaVmcp.png";
name = "20th Century Fox";
"origin_country" = US;
},
{
id = 4700;
"logo_path" = "/A32wmjrs9Psf4zw0uaixF0GXfxq.png";
name = "The Linson Company";
"origin_country" = "";
}
);
"production_countries" = (
{
"iso_3166_1" = DE;
name = Germany;
},
{
"iso_3166_1" = US;
name = "United States of America";
}
);
"release_date" = "1999-10-15";
revenue = 100853753;
runtime = 139;
"spoken_languages" = (
{
"iso_639_1" = en;
name = English;
}
);
status = Released;
tagline = "Mischief. Mayhem. Soap.";
title = "Fight Club";
video = 0;
"vote_average" = "8.4";
"vote_count" = 19688;
}