Skip to content

Instantly share code, notes, and snippets.

@shiningabdul
Last active November 17, 2017 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiningabdul/914597eef39506d5a07e61136c2be835 to your computer and use it in GitHub Desktop.
Save shiningabdul/914597eef39506d5a07e61136c2be835 to your computer and use it in GitHub Desktop.
import Foundation
protocol Transmittable : Codable {
}
extension Transmittable {
func encode() -> Data {
var data:Data = Data()
try? data = JSONEncoder().encode(self)
return data
}
static func decode(from data:Data?) -> Self? {
guard let data = data else {
return nil
}
let decoder = JSONDecoder()
let response = try? decoder.decode(self, from: data)
return response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment