Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Created June 11, 2017 09:36
Show Gist options
  • Save oleksii-demedetskyi/a7c3d58ebcc673642d8df2cd3b9b87fc to your computer and use it in GitHub Desktop.
Save oleksii-demedetskyi/a7c3d58ebcc673642d8df2cd3b9b87fc to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
struct Example: Codable {
let name: String
let age: Int
}
let example = Example(name: "Jonh", age: 30)
let data = try JSONEncoder().encode(example)
var restored = try JSONDecoder().decode(Example.self, from: data)
extension JSONDecoder {
func decode<T>(_ data: Data) throws -> T where T: Decodable {
return try decode(T.self, from: data)
}
}
/// Type of existed variable will be used
do { restored = try JSONDecoder().decode(data) }
/// Explicit variable will be used
do { let _ : Example = try JSONDecoder().decode(data) }
/// Explicit expression type will be used
do { let _ = try JSONDecoder().decode(data) as Example }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment