Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Last active March 28, 2018 14:51
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 Pasanpr/16c371aaf62dbdff7918eb57f6eea333 to your computer and use it in GitHub Desktop.
Save Pasanpr/16c371aaf62dbdff7918eb57f6eea333 to your computer and use it in GitHub Desktop.
Starter code for S2:V1 - Parsing Dates
import Foundation
let json = """
{
"title": "Harry Potter and the sorcerer's stone",
"url": "https:\\/\\/openlibrary.org\\/books\\/OL26331930M\\/Harry_Potter_and_the_sorcerer's_stone",
"publish_date": "1997-06-26T00:00:00+0000",
"text": "VGhpcyBpc24ndCByZWFsbHkgdGhlIGNvbnRlbnRzIG9mIHRoZSBib29r",
"rating": 4.9
}
""".data(using: .utf8)!
struct Book: Codable {
let title: String
let url: String
let publishDate: String
}
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let potter = try! decoder.decode(Book.self, from: json)
potter.title
potter.url
potter.publishDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment