我正在尝试解析以下 json 架构,海报可能为空,也可能不为空
{
"poster": {},
"recommends": []
}
我的可解码类如下:
public struct RecommendedList: Decodable {
public let poster: Poster?
public let recommends: [Recommend]
}
public struct Poster: Decodable {
public let backgroundImage: URL
public let topImage: URL
public let windowImage: URL
public let windowSkinImagePath: URL
public let deeplink: URL
public init(from decoder: Decoder) throws {
// I want a failable intializer not one that throws
}
}
我的问题是如何使海报可选?我的想法是我需要一个可失败的初始化程序,但可解码需要一个抛出的 init。