在嵌套中使用解码器时Codable
struct,有什么方法可以访问父结构的属性吗?
我能想到的唯一可行的方法(尚未测试)是在父结构中也使用手动解码器,在userInfo
字典,然后访问userInfo
在子结构中。但这会产生大量样板代码。我希望有一个更简单的解决方案。
struct Item: Decodable, Identifiable {
let id: String
let title: String
let images: Images
struct Images: Decodable {
struct Image: Decodable, Identifiable {
let id: String
let width: Int
let height: Int
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
width = try container.decode(Int.self, forKey: .width)
height = try container.decode(Int.self, forKey: .height)
// How do I get `parent.parent.id` (`Item#id`) here?
id = "\(parent.parent.id)\(width)\(height)"
}
}
let original: Image
let small: Image
// …
}
}
在上面的示例中,来自服务器的项目 ID 仅在 JSON 的顶级属性中定义,但我也需要在子级属性中定义它们,所以我也可以将它们设置为Identifiable
.
我使用 @New Dev 提到的 Itai Ferber 的建议进行了管理,方式如下:
- 创建一个新的引用类型,其唯一目的是包含
可以在父级和子级之间传递的可变值。
- 将该类型的实例分配给 JSONDecoder 的 userInfo 字典。
- 在解码父实例时检索该实例,并将您有兴趣传递的 id 分配给它。
- 在解码子级时,从先前存储在 userInfo 中的实例中检索该 id。
我已将您上面的示例修改如下:
struct Item: Decodable, Identifiable {
enum CodingKeys: String, CodingKey {
case id
case title
case images
}
let id: String
let title: String
let images: Images
struct Images: Decodable {
struct Image: Decodable, Identifiable {
let id: String
let width: Int
let height: Int
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
width = try container.decode(Int.self, forKey: .width)
height = try container.decode(Int.self, forKey: .height)
if let referenceTypeUsedOnlyToContainAChangeableIdentifier = decoder.userInfo[.referenceTypeUsedOnlyToContainAChangeableIdentifier] as? ReferenceTypeUsedOnlyToContainAChangeableIdentifier {
self.id = referenceTypeUsedOnlyToContainAChangeableIdentifier.changeableIdentifier
} else {
self.id = "something went wrong"
}
}
}
let original: Image
let small: Image
// …
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
if let referenceTypeUsedOnlyToContainAChangeableIdentifier = decoder.userInfo[.referenceTypeUsedOnlyToContainAChangeableIdentifier] as? ReferenceTypeUsedOnlyToContainAChangeableIdentifier {
referenceTypeUsedOnlyToContainAChangeableIdentifier.changeableIdentifier = id
}
}
}
}
// Use this reference type to just store an id that's retrieved later.
class ReferenceTypeUsedOnlyToContainAChangeableIdentifier {
var changeableIdentifier: String?
}
// Convenience extension.
extension CodingUserInfoKey {
static let referenceTypeUsedOnlyToContainAChangeableIdentifier = CodingUserInfoKey(rawValue: "\(ReferenceTypeUsedOnlyToContainAChangeableIdentifier.self)")!
}
let decoder = JSONDecoder()
// Assign the reference type here to be used later during the decoding process first to assign the id in `Item` and then
// later to retrieve that value in `Images`
decoder.userInfo[.referenceTypeUsedOnlyToContainAChangeableIdentifier] = ReferenceTypeUsedOnlyToContainAChangeableIdentifier()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)