Skip to content

Instantly share code, notes, and snippets.

@jbarros35
Created April 24, 2018 17:59
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 jbarros35/70649c0d8028a697764a2d06eea61407 to your computer and use it in GitHub Desktop.
Save jbarros35/70649c0d8028a697764a2d06eea61407 to your computer and use it in GitHub Desktop.
Swift JSON decode
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