Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (23 sloc) 715 Bytes
//: Playground - noun: a place where people can play
import UIKit
struct Hotel : Codable {
var name: String
var bookingDate: Date
}
let hotel = Hotel(
name: "Intercontinental",
bookingDate: Date()
)
let encoder = JSONEncoder()
if let data = try? encoder.encode(hotel) {
print(String(data: data, encoding: .utf8)!)
}
let jsonString = "{\"name\":\"Intercontinental\",\"bookingDate\":\"Friday, Aug 18, 2017\"}"
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMM d, yyyy"
decoder.dateDecodingStrategy = .formatted(formatter)
let data = jsonString.data(using: .utf8)!
if let data = try? decoder.decode(Hotel.self, from: data) {
print(data)
}