swift4 Codable Decodable Encodable
程序员文章站
2024-02-20 15:32:58
...
extension Encodable {
func toData() -> Data? {
let encoder = JSONEncoder()
return try?encoder.encode(self)
}
}
extension Decodable {
static func toModel(from data: Data) -> Self? {
let decoder = JSONDecoder()
return try? decoder.decode(Self.self, from: data)
}
}
转载于:https://www.jianshu.com/p/6f85edc4f0cb
上一篇: 1、数据类型和变量(认识提高)