嗯,我的问题很简单。我想和这个答案做同样的事情但是对于编码: How to decode a property with type of JSON dictionary in Swift 4 decodable protocol
该解决方案适用于解码,但我还需要编码Dictionary<String, Any>
我无法实现如何为KeyedEncodingContainer
和其他扩展编写扩展,以及要编写哪些方法。
答案 0 :(得分:0)
您可以通过根据该问题的答案调整“可解码”代码段来实现这一目标
extension KeyedEncodingContainerProtocol where Key == JSONCodingKeys {
mutating func encodeJSONDictionary(_ value: Dictionary<String, Any>) throws {
try value.forEach({ (key, value) in
let key = JSONCodingKeys(key: key)
switch value {
case let value as Bool:
try encode(value, forKey: key)
case let value as Int:
try encode(value, forKey: key)
case let value as String:
try encode(value, forKey: key)
case let value as Double:
try encode(value, forKey: key)
case let value as Dictionary<String, Any>:
try encode(value, forKey: key)
case let value as Array<Any>:
try encode(value, forKey: key)
case Optional<Any>.none:
try encodeNil(forKey: key)
default:
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath + [key], debugDescription: "Invalid JSON value"))
}
})
}
}
extension KeyedEncodingContainerProtocol {
mutating func encode(_ value: Dictionary<String, Any>, forKey key: Key) throws {
var container = self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
try container.encodeJSONDictionary(value)
}
mutating func encodeIfPresent(_ value: Dictionary<String, Any>?, forKey key: Key) throws {
if let value = value {
try encode(value, forKey: key)
}
}
mutating func encode(_ value: Array<Any>, forKey key: Key) throws {
var container = self.nestedUnkeyedContainer(forKey: key)
try container.encodeJSONArray(value)
}
mutating func encodeIfPresent(_ value: Array<Any>?, forKey key: Key) throws {
if let value = value {
try encode(value, forKey: key)
}
}
}
extension UnkeyedEncodingContainer {
mutating func encodeJSONArray(_ value: Array<Any>) throws {
try value.enumerated().forEach({ (index, value) in
switch value {
case let value as Bool:
try encode(value)
case let value as Int:
try encode(value)
case let value as String:
try encode(value)
case let value as Double:
try encode(value)
case let value as Dictionary<String, Any>:
try encode(value)
case let value as Array<Any>:
try encode(value)
case Optional<Any>.none:
try encodeNil()
default:
let keys = JSONCodingKeys(intValue: index).map({ [ $0 ] }) ?? []
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath + keys, debugDescription: "Invalid JSON value"))
}
})
}
mutating func encodeJSONDictionary(_ value: Dictionary<String, Any>) throws {
var nestedContainer = self.nestedContainer(keyedBy: JSONCodingKeys.self)
try nestedContainer.encodeJSONDictionary(value)
}
}
这是
的代码段但是,此代码段将在Swift 4.1中失败,该版本应在2018年上半年发布。