- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确实在堆栈溢出中看到了很多类似的问题,但似乎没有人与我的情况相似。我是结合框架工作的新手,我花了一整个下午才弄清楚出了什么问题,但仍然停留在这里......
Xcode 给了我以下错误,我所做的是使用 TMDB 的 API 并将其解码为我的 Actor 模型。它在这条线上失败 let result = try self.decoder.decode(TMDBActorsResult.self, from: output.data)
.你能给我一些提示吗 adult
?
ERROR: keyNotFound(CodingKeys(stringValue: "adult", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "knownFor", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"adult\", intValue: nil) (\"adult\").", underlyingError: nil))
import SwiftUI
struct TMDBActorsResult: Codable {
let page: Int?
let results: [Actor]
let totalResults: Int?
let totalPages: Int?
}
struct Actor: Codable {
let profilePath: String?
let adult: Bool
let id: Int?
let name: String?
let popularity: CGFloat?
let knownFor: [Production]?
let knownForDepartment: String?
let gender: Int?
}
// MARK: Used for two objects with media type = (Movie or TV)
struct Production: Codable {
let posterPath: String?
let adult: Bool
let overview: String?
let releaseDate: String?
let originalTitle: String?
let genreIds: [Int]?
let id: Int?
let mediaType: String?
let originalLanguage: String?
let title: String?
let backdropPath: String?
let popularity: Double?
let voteCount: Int?
let video: Bool
let voteAverage: Double?
let firstAirDate: String?
let originCountry: [String]?
let name: String?
let originalName: String?
}
import Foundation
import Combine
enum HTTPError: LocalizedError {
case statusCode
case post
}
struct WebService {
private var decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()
private var session: URLSession = {
let config = URLSessionConfiguration.default
config.urlCache = URLCache.shared
config.waitsForConnectivity = true
config.requestCachePolicy = .reloadIgnoringLocalCacheData
return URLSession(configuration: config, delegate: nil, delegateQueue: nil)
}()
private func createPublisher<T: Codable>(for url: URL) -> AnyPublisher<T, Error> {
print("Pblisher URL: \(url)")
return session.dataTaskPublisher(for: url)
.tryMap { output in
guard let response = output.response as? HTTPURLResponse, response.statusCode == 200 else {
print("Response: \(output.response)")
do {
let ss = try self.decoder.decode(Response.self, from: output.data)
print("ss: \(ss)")
} catch {
print(error)
}
throw HTTPError.statusCode
}
do {
let result = try self.decoder.decode(TMDBActorsResult.self, from: output.data)
print("Result: \(result)")
} catch {
print("ERROR: \(error)")
}
return output.data
}
.decode(type: T.self, decoder: decoder)
.eraseToAnyPublisher()
}
func getSectionsPublisher() -> AnyPublisher<TMDBActorsResult, Error> {
createPublisher(for: TMDBClient.Endpoints.popularActors.url).eraseToAnyPublisher()
}
}
Instance method 'decode(_:forKey:)' requires that 'MediaType' conform to 'Decodable'
.我发布说不同的是我的模型正在使用
type
在项目中,但不在根媒体结构中。
let jsonString = """
[{"name": "Popular Movies",
"description": "Basic movie description",
"items": [ { "id": 15, "budget": 10, "type": "movies","name": "Sample movie", "movieSPT": ""}]
},
{"name": "Popular TV Shows",
"description": "Basic shows description",
"items": [ { "id": 15, "adult": false, "type": "tvshows","title": "Sample show", "showSPT": ""}]
}
]
"""
let data = Data(jsonString.utf8)
struct Movie : Decodable {
let id: Int
let name, movieSPT: String
let type: String
let budget: Int
}
struct TVShow : Decodable {
let id: Int
let title, showSPT: String
let type: String
let adult: Bool
}
enum MediaType {
case movie([Movie]), tvShow([TVShow])
}
struct Media : Decodable {
let name : String
let description : String
let items : MediaType
private enum CodingKeys : String, CodingKey { case name, description, type, items }
init(from decoder : Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.description = try container.decode(String.self, forKey: .description)
self.items = try container.decode(MediaType.self, forKey: .items)
// type = try container.decode(String.self, forKey: .type)
if items.type == "movies" {
let movieData = try container.decode([Movie].self, forKey: .items)
// print("AAA: \(movieData)")
items = .movie(movieData)
} else { // add better error handling
let showData = try container.decode([TVShow].self, forKey: .items)
items = .tvShow(showData)
}
}
}
do {
let result = try JSONDecoder().decode([Media].self, from: data)
print(result)
} catch {
print(error)
}
known_for
是一个[对象],该对象可以是电影类型或电视类型。
{
"page": 1,
"total_results": 10000,
"total_pages": 500,
"results": [
{
"popularity": 57.168,
"known_for_department": "Acting",
"name": "Thassapak Hsu",
"id": 1910848,
"profile_path": "/1fmjgN8EvDj1TiEJk2Zs4y0T40O.jpg",
"adult": false,
"known_for": [
{
"original_name": "萌妻食神",
"genre_ids": [
35,
10765,
10766
],
"media_type": "tv",
"name": "Cinderella Chef",
"origin_country": [],
"vote_count": 5,
"first_air_date": "2018-04-23",
"backdrop_path": "/rnzmWKEiWPb8GC1mlqQojj8ccWj.jpg",
"original_language": "zh",
"id": 79574,
"vote_average": 9,
"overview": "",
"poster_path": "/xb40Li6ff1BK0pVOxV4lutssCrR.jpg"
},
{
"original_name": "外星女生柴小七",
"genre_ids": [
35,
10765
],
"media_type": "tv",
"name": "My Girlfriend is an Alien",
"origin_country": [
"CN"
],
"vote_count": 2,
"first_air_date": "2019-08-19",
"backdrop_path": "/kCl7piWv3pypgYfyLFi7ZgFGlYV.jpg",
"original_language": "zh",
"id": 92779,
"vote_average": 9,
"overview": "The alien girl Chai Xiaoqi tells the story of Fang Xiaoqi, the overbearing president of the alien girl who died from the \"Cape Town Planet\", who was suffering from the \"rainy weather heterosexual amnesia\". A high-energy hilarious and romantic cross-star love story. The female host Chai Xiaoqi is not only an alien, but also a true-handed witch. Once she inhales the hormones emitted by the males in the earth, she will fall into the \"flowery state\" and suffer from various diseases. The fun and ridiculously ridiculous romance will restore the singularity of the girl in the perfection of the girl. In order to survive on the human earth, Chai Xiaoqi will use his various super powers to solve one accident after another, like a roller coaster. The ups and downs will make the audience hooked. The male lord is cold and is an alternative overbearing president. When it rains, he will forget the opposite sex that appears around him. For this reason, he and the female host will launch various \"fighting and fighting\" laughter dramas. The experience of high sweetness and romance is expected to be Strongly slammed the girl's heart when it was broadcast.",
"poster_path": "/5e2owvs9TWVsuIacTFxJGPp6KVW.jpg"
},
{
"original_name": "Devil Lover เผลอใจ..ให้นายปีศาจ",
"id": 74640,
"media_type": "tv",
"name": "Devil Lover เผลอใจ..ให้นายปีศาจ",
"vote_count": 0,
"vote_average": 0,
"first_air_date": "2015-10-07",
"poster_path": "/moThN7iERydEHI2RbfrmhCp69R4.jpg",
"genre_ids": [
35
],
"original_language": "th",
"backdrop_path": "/iRYOwW6DRIRwDYVmRWA8nbfaV2c.jpg",
"overview": "",
"origin_country": [
"TH"
]
}
],
"gender": 2
},
{
"popularity": 39.35,
"known_for_department": "Acting",
"gender": 1,
"id": 2487703,
"profile_path": "/jRdDoFoHq36hg4kYxxiLa5DRYUW.jpg",
"adult": false,
"known_for": [
{
"poster_path": "/d9PhCnofBEeQGR3lwywTjWKBiXj.jpg",
"id": 449924,
"vote_count": 346,
"video": false,
"media_type": "movie",
"adult": false,
"backdrop_path": "/ekP6EVxL81lZ4ivcqPsoZ72rY0h.jpg",
"genre_ids": [
28,
18,
36
],
"original_title": "葉問4",
"original_language": "cn",
"title": "Ip Man 4: The Finale",
"vote_average": 6,
"overview": "Following the death of his wife, Ip Man travels to San Francisco to ease tensions between the local kung fu masters and his star student, Bruce Lee, while searching for a better future for his son.",
"release_date": "2019-12-20"
}
],
"name": "Vanda Lee"
},
{
"popularity": 28.664,
"known_for_department": "Acting",
"gender": 1,
"id": 556435,
"profile_path": "/5MgWM8pkUiYkj9MEaEpO0Ir1FD9.jpg",
"adult": false,
"known_for": [
{
"release_date": "2019-05-30",
"id": 496243,
"vote_count": 5120,
"video": false,
"media_type": "movie",
"vote_average": 8.6,
"title": "Parasite",
"genre_ids": [
35,
18,
53
],
"original_title": "기생충",
"original_language": "ko",
"adult": false,
"backdrop_path": "/TU9NIjwzjoKPwQHoHshkFcQUCG.jpg",
"overview": "All unemployed, Ki-taek's family takes peculiar interest in the wealthy and glamorous Parks for their livelihood until they get entangled in an unexpected incident.",
"poster_path": "/7IiTTgloJzvGI1TAYymCfbfl3vT.jpg"
},
....
最佳答案
错误说:
键 adult
没有值在数组 Index 0
的第一项( knownFor
)中( [Production]
) 位于数组 Index 0
的第一项 ( results
) 中( Actor
)。
请检查,屏幕截图仅显示 [...]
关于您的编辑:
你不能那样解码嵌套字典,请试试这个
struct TMDBActorsResult: Decodable {
let page: Int
let results: [Actor]
let totalResults: Int
let totalPages: Int
}
struct Actor: Decodable {
let popularity: Double
let knownForDepartment: String
let gender: Int
let profilePath: String
let name: String?
let id: Int
let adult: Bool
let knownFor: [Production]
}
enum MediaType : String, Decodable {
case tv, movie
}
// MARK: Used for two objects with media type = (Movie or TV)
enum Production : Decodable {
case movie(Movie), tvShow(TVShow)
private enum CodingKeys : String, CodingKey { case mediaType }
init(from decoder : Decoder) throws {
let dictionaryContainer = try decoder.container(keyedBy: CodingKeys.self)
let mediaType = try dictionaryContainer.decode(MediaType.self, forKey: .mediaType)
let container = try decoder.singleValueContainer()
switch mediaType {
case .tv: self = .tvShow(try container.decode(TVShow.self))
case .movie: self = .movie(try container.decode(Movie.self))
}
}
}
struct Movie: Decodable {
let posterPath: String
let adult: Bool
let voteCount: Int
let video: Bool
let backdropPath: String
let genreIds: [Int]
let originalTitle: String
let originalLanguage: String
let title: String
let voteAverage: Double
let overview: String
let releaseDate: String
}
struct TVShow: Decodable {
let originalName: String
let genreIds: [Int]
let name: String
let originCountry: [String]
let backdropPath: String
let voteCount: Int
let firstAirDate: String
let originalLanguage: String
let overview: String
let posterPath: String
let id: Int
}
关于Swift: 错误: keyNotFound(CodingKeys(stringValue: "adult", intValue: nil),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60633154/
在 int 列上,做...之间有区别吗? SELECT .. int = '1' 和 SELECT .. int = 1,哪个更好? 最佳答案 使用int = 1,因为int = '1'在内部转换为i
在开发我的应用程序时,我遇到了一些比较的东西: string str = "12345"; int j = 12345; if (str == j.ToString())
嘿,我有一个数字数组列表,当我尝试转换为 int 时,我得到 The method intValue() is undefined for the type Object 错误。这是代码片段。 whi
我有一个用 Java 计算 Lisp 表达式的程序,但我遇到了一个奇怪的错误。 首先,这是程序: import java.util.Queue; import java.util.LinkedList
我在大学的一项任务中确实面临着一个问题: public class Num { Integer x; Integer y; public Num(Integer x, Inte
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在解决一个问题,我不明白为什么它不起作用。如何通过 double obj 传递变量并转换为 int? 为什么它在顶部代码片段中不起作用,但在该行下方的底部代码片段中起作用? 唯一的区别似乎是添加了
我正在尝试为私有(private)双变量创建一个 get 方法。 当我写作时 public class { private double x = 4.12; public int get(){
我有一个 Number 对象并调用“intValue()”,它将返回一个 Integer 值。但是,如果该值大于 Integer.MAX_VALUE,则该方法将进行舍入。没关系,但我想知道函数是否轮换
如果 UIText 字段中输入的值低于 200,则会生成警报。 如果用户在字段中添加 > 以表示其大于 200,则仍会触发警报。显然我不想要这个,所以我需要某种方法来忽略这个标点符号,甚至更好地忽略所
考虑下面的代码 id j = @"123"; NSUInteger a = [j unsignedIntValue]; // not working NSUInteger b = [j intValu
我使用以下代码获取最大值: NSArray *allValues = @[ [NSNumber numberWithInt:20], [NSNumbe
我在使用 BigDecimal 类的 intValue() 时遇到了一个奇怪的 java 错误。 我的起始值为 3300028000。 当我调用下面的代码时: int i = d.intValue()
我有一个 swift 3 的问题,它不会出现在 swift 2 上。我读取一个 json 值,并调用函数 intValue (integerValue of swift 2 ) 和 xcode 我得到
这段代码 Double dbl = 254.9999999999999; Integer integ = dbl.intValue(); System.out.println(integ); 显示 2
显式编码拆箱已包装的原始数值。在Java5及以上的版本,拆箱是不必要的,可以安全地删除。那么 JDK5 到底做了啥? 自动装箱(auto-boxing)与自动拆箱(auto-unboxing) Jav
我创建了一个如下所示的 IBAction: - (IBAction)okButton:(id)sender { NSLog(@"%d",[sender intValue]); } 当我重复单击
import java.util.ArrayList; public class Averager { private ArrayList list; public Averager()
NSString 的文档没有提到调用 intValue 方法时引发的任何异常。如果没有其他具有 Objective-C 专业知识的人确认即使字符串是字母数字也不会引发异常,我觉得有点难以置信。 (ps
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { int num = 5; [viewControl
我是一名优秀的程序员,十分优秀!