Skip to content

Instantly share code, notes, and snippets.

@ykpoh
Created May 29, 2021 03:45
Show Gist options
  • Save ykpoh/5ba0778fba2e256e273d1304d3b179be to your computer and use it in GitHub Desktop.
Save ykpoh/5ba0778fba2e256e273d1304d3b179be to your computer and use it in GitHub Desktop.
struct Launch: Codable, Equatable {
let id: String?
let name: String?
let details: String?
let date_utc: Date?
let upcoming: Bool?
let success: Bool?
let rocket: String?
init(id: String?, name: String?, details: String?, date_utc: Date?, upcoming: Bool?, success: Bool?, rocket: String?) {
self.id = id
self.name = name
self.details = details
self.date_utc = date_utc
self.upcoming = upcoming
self.success = success
self.rocket = rocket
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decodeIfPresent(String.self, forKey: .id)
name = try container.decodeIfPresent(String.self, forKey: .name)
details = try container.decodeIfPresent(String.self, forKey: .details)
upcoming = try container.decodeIfPresent(Bool.self, forKey: .upcoming)
success = try container.decodeIfPresent(Bool.self, forKey: .success)
rocket = try container.decodeIfPresent(String.self, forKey: .rocket)
if let dateString = try container.decodeIfPresent(String.self, forKey: .date_utc) {
date_utc = DateFormatter.iso8601Full.date(from: dateString)
} else {
date_utc = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment