VDCodable
描述
此仓库包含一些用于 Codable
协议和数据解码的有用工具。
使用方法
JSON
JSON
枚举使得处理 JSON 数据变得简单。使用 String
、Int
下标和动态成员查找(“点”语法)来检索值
if let name = json.root.array[0]?.name.string {...}
//or if let name = json["root"]["array"][0]["name"]?.string {...}
JSON
枚举完全基于 Swift Protobuf 实现,基于 Swift JSON 序列化,非常快。符合 Codable
。
VDJSONDecoder
一个对象,它从 JSON 对象解码数据类型的实例。与 Foundation JSONDecoder
的主要区别
- 从引号中的值(如 "true"、"0.0")解码非字符串类型
- 通过
(([CodingKey], JSON) -> JSON)
闭包进行自定义 JSON 解析 - 纯 Swift 和更快
VDJSONEncoder
纯 Swift 的 JSONEncoder
版本。
URLQueryEncoder
和URLQueryDecoder
查询字符串的编码器和解码器。
struct SomeStruct: Codable {
var title = "Query_string"
let number = 0
}
let baseURL = URL(string: "https://base.url")!
let value = SomeStruct()
let url = try? URLQueryEncoder().encode(value, for: baseURL)
//url = "https://base.url?title=Query_string&number=0"
-
DictionaryDecoder
和DictionaryEncoder
-
NSManagedDecodable
、NSManagedEncodable
和NSManagedCodable
协议
使您的 NSManagedObject
子类符合 Codable
协议的协议。
PlainCodingKey
简单的 CodingKey
结构体。
Decodable
类型的类型反映工具
let properties: [String: Any.Type] = Mirror.reflect(SomeType.self)
//or Mirror(SomeType.self).children
- 创建自定义编码器/解码器的工具
基于编写不同的编码器/解码器时的类似逻辑,实现了DecodingUnboxer
和EncodingBoxer
协议。本库中的所有编码器和解码器的使用示例。
安装
将以下行添加到您的 Podfile 文件中
pod 'VDCodable'
然后首先从 podfile 目录运行 pod update
。
创建一个 Package.swift
文件。
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDCodable.git", from: "0.13.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDCodable"])
]
)
$ swift build
作者
Voidilov, [email protected]
许可
VDCodable 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。