Created
June 11, 2017 09:36
-
-
Save oleksii-demedetskyi/a7c3d58ebcc673642d8df2cd3b9b87fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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