【文件属性】:
文件名称:DefaultCodable:使用Swift Codable类型处理默认值的便捷方法
文件大小:9KB
文件格式:ZIP
更新时间:2021-03-21 19:59:35
swift defaults codable property-wrapper Swift
默认编码
DefaultCodable是一个Swift µpackage,它提供了一种方便的方法来为不存在的属性或具有nil值的属性在Codable类型中定义默认值。
用法
考虑一个针对Apple产品的假设模型,其中仅需要提供属性name 。
enum ProductType : String , Codable , CaseIterable {
case phone , pad , mac , accesory
}
struct Product : Codable {
var name: String
var description: String ?
var isAvailable: Bool ?
var type: ProductType ?
}
使用@Default属性包装器,我们可以为不需要的属性提供默认值,从而摆脱模型中的可选参数。
struct Pr
【文件预览】:
DefaultCodable-master
----.gitignore(53B)
----Package.swift(388B)
----README.md(4KB)
----.swiftpm()
--------xcode()
----Sources()
--------DefaultCodable()
----LICENSE(1KB)
----.swift-version(4B)
----Tests()
--------LinuxMain.swift(130B)
--------DefaultCodableTests()