Skip to content

Instantly share code, notes, and snippets.

@fitomad fitomad/codable_encode.swift Secret
Last active Feb 6, 2019

Embed
What would you like to do?
/// 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
You can’t perform that action at this time.