BoxData
BoxData 是一个轻量级的字节格式数据序列化库。使用 Box,您可以将 Codable
数据压缩成比 JSON 或 Plist 轻几千倍,并且读写速度也比它们快。
示例
当保存以下数据时。
类型 | 文件大小 |
---|---|
Box | 155 B !!!!! |
JSON | 5.8 MB |
Plist | 5.4 MB |
// Codable Data
struct Person: Codable {
let name: String
let age: UInt8
let birth:Conutry
struct Conutry: Codable {
let name: String
let id: UInt8
}
}
// Prepare Data
let alice = Person(name: "Alice", age: 16, birth: .init(name: "UK" , id: 12))
let bob = Person(name: "Bob" , age: 22, birth: .init(name: "America", id: 14))
/// 100000 data !!!
let people = Array(repeating: alice, count: 50000)
+ Array(repeating: bob , count: 50000)
用法
您可以使用 BoxEncoder
和 BoxDecoder
,就像使用 JSONEncoder
或 PropertyListEncoder
一样。
do {
// encoding
let data = try BoxEncoder().encode(people)
// decoding
let decoded = try BoxDecoder().decode(Array<Person>.self, from: data)
} catch {
print(error)
}
要运行示例项目,请克隆仓库,然后从 Example 目录首先运行 pod install
。
选项
Box 格式有 2 个选项。
-
useStructureCache
此选项启用结构缓存。
-
useCompression
此选项启用压缩。
let encoder = BoxEncoder()
// set before encode
encoder.useStructureCache = true / false
encoder.useCompression = true / false
安装
BoxData 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile 中
pod 'BoxData'
作者
ObuchiYuki,[email protected]
许可
BoxData 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。