为 Codable 提供的样本保存和加载
用法
假设您有一个某些 Dummy
结构是 Codable
的
struct Dummy: Codable {
let text: String
}
现在您可以轻松将其保存到应用程序文档目录中
let dummy = Dummy(text: "This is an example")
dummy.save()
//or if you what to know when it finished:
dummy.save().done { _ in
print("💾 Saved!")
}.catch { error in
print("💣 \(error)")
}
然后您可以再次将该数据加载到结构中
Dummy.load().done { dummy in
print(dummy.text)
}.catch { error in
print("💣 \(error)")
}
或者您可以删除该文件
Dummy.delete()
//or
Dummy.delete().done { _ in
print("deleted")
}.catch { error in
print("💣 \(error)")
}
安装
使用 CocoaPods。
将其添加到您的 Podfile 中
pod 'CodableSaveLoad'
然后调用
pod install
并导入
import CodableSaveLoad