Created
April 15, 2022 10:09
-
-
Save olmedocr/bb5ae4a7f19cf01a7cdd598618de3f63 to your computer and use it in GitHub Desktop.
codable model with struct to have more control over the encoding/decoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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