Skip to content

Instantly share code, notes, and snippets.

@olmedocr
Created April 15, 2022 10:09
Show Gist options
  • Save olmedocr/bb5ae4a7f19cf01a7cdd598618de3f63 to your computer and use it in GitHub Desktop.
Save olmedocr/bb5ae4a7f19cf01a7cdd598618de3f63 to your computer and use it in GitHub Desktop.
codable model with struct to have more control over the encoding/decoding
struct MediaContainerModel<Type: Decodable & MediaContainerizable>: Decodable {
var title1: String
var content: [Type]
struct CodingKeys: CodingKey {
init?(intValue: Int) {
return nil
}
init(stringValue: String) {
self.stringValue = stringValue
}
let stringValue: String
let intValue: Int? = nil
static var title1: Self { Self(stringValue: "title1") }
static var content: Self { Self(stringValue: Type.contentType.rawValue) }
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.title1 = try container.decode(String.self, forKey: CodingKeys.title1)
self.content = try container.decode([Type].self, forKey: CodingKeys.content)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment