欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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