Instantly share code, notes, and snippets.

@fitomad fitomad/codable_final.swift Secret
Created Feb 6, 2019

Embed
What would you like to do?
//
// JSON -> [Version]
//
var versions: [Version]?
if let jsonURL = URL(string: "https://raw.githubusercontent.com/fitomad/Desappstre-Codable/master/JSON/osx-macos.json"),
let data = try? Data(contentsOf: jsonURL)
{
// Creamos el decodificador JSON
let decoder = JSONDecoder()
// Establecemos el formato de fechas
decoder.dateDecodingStrategy = .formatted(DateFormatter.versionFormatter)
do
{
// Obtenemos el array de Version
versions = try decoder.decode([Version].self, from: data)
// Lo recorremos
versions?.forEach({
print("\($0.codeName). Última versión \($0.lastRelease)")
})
}
catch let jsonError
{
print(jsonError.localizedDescription)
}
}
//
// Version -> JSON
//
if let versions = versions
{
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(DateFormatter.versionFormatter)
if let version = versions.filter({ $0.codeName == "Mojave" }).first
{
let data = try? encoder.encode(version)
dump(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment