-
-
Save fitomad/bc6ee74378d49ba2753fb0cb14ae0107 to your computer and use it in GitHub Desktop.
This file contains 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
/// Para decodificar un JSON | |
public init(from decoder: Decoder) throws | |
{ | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
if let initialValue = try container.decodeIfPresent(Release.HelperInitial.self, forKey: .initial) | |
{ | |
self = .initial(os: initialValue.os, version: initialValue.version) | |
} | |
else if let latestValue = try container.decodeIfPresent(Release.HelperLatest.self, forKey: .latest) | |
{ | |
self = .latest(os: latestValue.os, version: latestValue.version, update: latestValue.update) | |
} | |
else | |
{ | |
throw VersionError.jsonDecoding | |
} | |
} | |
/// Para codificar la enumeración en un JSON | |
public func encode(to encoder: Encoder) throws | |
{ | |
var container = encoder.container(keyedBy: CodingKeys.self) | |
switch self | |
{ | |
case .initial(let os, let version): | |
let initialStruct = HelperInitial(os: os, version: version) | |
try container.encode(initialStruct, forKey: .initial) | |
case .latest(let os, let version, let update): | |
let latestStruct = HelperLatest(os: os, version: version, update: update) | |
try container.encode(latestStruct, forKey: .latest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment