Xcode | Minimun Deployments | Version |
---|---|---|
Xcode15 | >= iOS13 / macOS11 | 1.0.0 |
< Xcode15 | < iOS13 / macOS11 | 1.0.1 |
The project objective is to enhance the usage experience of the Codable protocol using the macro provided by Swift 5.9 and to address the shortcomings of various official versions.
- Transformer
pod 'SweetCodable', '1.0.1'
https://github.com/Jowsing/SweetCodable
@Codable
struct Demo {
var a: String = "hello world"
var b: String = Bool.random() ? "hello world" : ""
let c: String
let d: String?
let e: Int?
@CodingKey("hello")
var hi: String = "ok"
}
-
Auto conformance
Codable
protocol if not explicitly declared// both below works well @Codable struct Demo {} @Codable struct Demo: Codable {}
-
Default value
@Codable struct Test { let name: String var balance: Double = 0 } // { "name": "jowsing" }
-
Basic type automatic convertible, between
String
Bool
Number
etc.@Codable struct Test { let num: Int? } // { "num": "911" }
-
Default Init
@Codable public struct Test { public var name: String = "jowsing" // Automatic generated public init() { self.name = "jowsing" } }
-
Custom
CodingKey
s@Codable struct Test { @CodingKey("s1", "s2", "s4") var str: String = "" } // { "s4": "jowsing" }
-
Automatic generate
Codable
class's subclassinit(from:)
andencode(to:)
super calls@Codable class BaseModel { let name: String } @SubCodable class SubModel: BaseModel { let age: Int } // {"name": "jowsing", "age": 22}