美文网首页
swift4 Codable Decodable Encodab

swift4 Codable Decodable Encodab

作者: LJ的ios开发 | 来源:发表于2018-07-24 10:37 被阅读114次
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)
    }
}

相关文章

网友评论

      本文标题:swift4 Codable Decodable Encodab

      本文链接:https://www.haomeiwen.com/subject/qrfduxtx.html