Created
April 24, 2018 17:59
-
-
Save jbarros35/70649c0d8028a697764a2d06eea61407 to your computer and use it in GitHub Desktop.
Swift JSON decode
This file contains hidden or 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
struct Event: Decodable { | |
let id: String | |
let event: String | |
let data: [[Int]]? | |
let date: String? | |
} | |
func decode() { | |
let json = """ | |
{ | |
"id": "b1afdc30-47e7-11e8-8931-33f1426b7c32", | |
"event": "move", | |
"data" : [[1,1]], | |
"date" : "\(Date())" | |
} | |
""".data(using: .utf8)! | |
guard let event = try? JSONDecoder().decode(Event.self, from: json) else { | |
print("Error: Message format not known") | |
return | |
} | |
print(event) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment