【发布时间】:2018-03-15 20:44:40
【问题描述】:
Swift 4. 我的情况与Using Codable on a dynamic type/object 非常相似,但对我来说,变化的变量是字典的名称,而不是里面的键。它看起来像:
{
"customName": {
"constantKey": Double,
"constantKey2": Double,
}
}
这是我要更改的代码,它已被提议作为另一个问题的答案,我做了很少的更改:
struct GenericCodingKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }
static func makeKey(name: String) -> GenericCodingKeys {
return GenericCodingKeys(stringValue: name)!
}
}
struct MyModel: Decodable {
var customName: [String: Double]
private enum CodingKeys: String, CodingKey {
case customName
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
customName = [String: String]()
let subContainer = try container.nestedContainer(keyedBy: GenericCodingKeys.self, forKey: .customName)
for key in subContainer.allKeys {
customName[key.stringValue] = try subContainer.decode(Double.self, forKey: key)
}
}
}
这是我得到的明显错误,因为我不知道如何更改此自定义名称:keyNotFound(Testapp.MyModel.(CodingKeys in _7A951077E4B6EF2E56D367C5DE0BF0AC).customName, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get KeyedDecodingContainer<GenericCodingKeys> -- no value found for key \"customName\"", underlyingError: nil))
【问题讨论】: