swift4 Codable Decodable Encodable

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 Encodable)