Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created September 15, 2018 22:24
Show Gist options
  • Save azamsharp/29d26b4159a3186a376462092da8dde9 to your computer and use it in GitHub Desktop.
Save azamsharp/29d26b4159a3186a376462092da8dde9 to your computer and use it in GitHub Desktop.
Category Structure
struct Category :Decodable {
let categoryName :String
let detail :[Detail]
struct Detail :Decodable {
let category :String
let trailerPrice :String
let isWatchlist :Bool?
}
private struct CodingKeys :CodingKey {
var stringValue :String
var intValue :Int? {
return nil
}
init?(stringValue :String) {
self.stringValue = stringValue
}
init?(intValue :Int) {
return nil
}
static let categoryName = CodingKeys(stringValue: "categoryName")!
}
init(from decoder :Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.categoryName = try container.decode(String.self, forKey: .categoryName)
let key = CodingKeys(stringValue: self.categoryName)!
self.detail = try container.decode([Detail].self, forKey: key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment