WolfAnyCodable
Swift 类型擦除包装器,用于 Encodable、Decodable 和 Codable 值。
本 Cocoapod 的存在是为了轻松整合 AnyCodable 的功能。
该功能在 Flight School Swift Codable 指南的第 3 章中有所讨论。
AnyEncodable
import AnyCodable
let dictionary: [String: AnyEncodable] = [
"boolean": true,
"integer": 1,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": [
"a": "alpha",
"b": "bravo",
"c": "charlie"
]
]
let encoder = JSONEncoder()
let json = try! encoder.encode(dictionary)
AnyDecodable
let json = """
{
"boolean": true,
"integer": 1,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
}
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let dictionary = try! decoder.decode([String: AnyDecodable].self, from: json)
AnyCodable
AnyCodable
可用于包装编码和解码的值。
许可
MIT
原始作者
Mattt (@mattt)
需求
Swift 4.2
安装
WolfAnyCodable 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中。
pod 'WolfAnyCodable'
作者
Wolf McNally, [email protected]
许可协议
WolfAnyCodable 可在 MIT 许可协议下使用。更多信息请参阅 LICENSE 文件。