Skip to content

Instantly share code, notes, and snippets.

@fitomad
Last active February 6, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/bc6ee74378d49ba2753fb0cb14ae0107 to your computer and use it in GitHub Desktop.
Save fitomad/bc6ee74378d49ba2753fb0cb14ae0107 to your computer and use it in GitHub Desktop.
/// 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