gpt4 book ai didi

ios - TMDb api 调用 - Swift

转载 作者:行者123 更新时间:2023-12-01 21:43:15 25 4
gpt4 key购买 nike

我调用 TMDb Api按类型获取一系列电影。我想访问的许多属性(例如“vote_count”、“poster_path”和“vote_average”)都返回“nil”。

如果我在浏览器中调用 api,我会按预期获得所有属性。

这是我的模型:

import Foundation

// MARK: - MovieList
struct MovieList: Codable {
let page: Int
let totalResults: Int?
let totalPages: Int?
let results: [Result]

enum CodingKeys: String, CodingKey {
case page
case totalResults = "total_results"
case totalPages = "total_pages"
case results
}
}

// MARK: - Result
struct Result: Codable {
let popularity: Double?
let voteCount: Int?
let video: Bool?
let posterPath: String?
let id: Int?
let adult: Bool?
let backdropPath: String?
let originalLanguage: OriginalLanguage?
let originalTitle: String?
let genreIDS: [Int]?
let title: String?
let voteAverage: Double?
let overview, releaseDate: String?

enum CodingKeys: String, CodingKey {
case popularity
case voteCount = "vote_count"
case video
case posterPath = "poster_path"
case id, adult
case backdropPath = "backdrop_path"
case originalLanguage = "original_language"
case originalTitle = "original_title"
case genreIDS = "genre_ids"
case title
case voteAverage = "vote_average"
case overview
case releaseDate = "release_date"
}
}

enum OriginalLanguage: String, Codable {
case en = "en"
case es = "es"
}

这是网络调用:
func getMovieDetails(movie: Int, completion: @escaping (Result?) -> ()) {

guard let url = URL(string: "https://api.themoviedb.org/3/movie/157336?api_key=6228bff945f7bd2m18c04fc3839829c0") else {
fatalError("Invalid URL")
}

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)

let task = session.dataTask(with: url) { data, response, error in

// Check for errors
guard error == nil else {
print ("error: \(error!)")
return
}
// Check that data has been returned
guard let data = data else {
print("No data")
return
}

do {

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let movieDetails = try decoder.decode(Result.self, from: data)

DispatchQueue.main.async {

completion(movieDetails)
print(movieDetails)

}

} catch let err {
print("Err", err)
}
}
// execute the HTTP request
task.resume()
}
}

这是回应:
MovieList(page: 1, totalResults: nil, totalPages: nil, results: [QuickFlicks.Result(popularity: Optional(171.78), voteCount: nil, video: Optional(false), posterPath: nil, id: Optional(454626), adult: Optional(false), backdropPath: nil, originalLanguage: nil, originalTitle: nil, genreIDS: nil, title: Optional("Sonic the Hedgehog"), voteAverage: nil, overview: Optional("Based on the global blockbuster videogame franchise from Sega, Sonic the Hedgehog tells the story of the world’s speediest hedgehog as he embraces his new home on Earth. In this live-action adventure comedy, Sonic and his new best friend team up to defend the planet from the evil genius Dr. Robotnik and his plans for world domination."), releaseDate: nil)])

任何帮助,将不胜感激。谢谢你。

最佳答案

您向解码器提供了相互矛盾的指令,首先您有 CodingKeys 枚举,例如 posterPath属性应该从 poster_path 中读取键,然后你设置 keyDecodingStrategy = .convertFromSnakeCase这意味着解码器首先翻译 key poster_pathposterPath在尝试将键与属性匹配之前。

所以要么删除 decoder.keyDecodingStrategy = .convertFromSnakeCase或删除 CodingKeys枚举

关于ios - TMDb api 调用 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62396033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com