From 9a42b19545bf8aa0cef62ff26efd0cdccbb01dc0 Mon Sep 17 00:00:00 2001 From: Shawn Moore Date: Wed, 22 Nov 2017 15:58:08 -0600 Subject: [PATCH] Modification of CodingKeys to full capitalization --- XMLParsing/Sample XML/CDs/CDCatalog.swift | 11 ++++++++++- XMLParsing/Sample XML/Plants/PlantCatalog.swift | 17 ++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/XMLParsing/Sample XML/CDs/CDCatalog.swift b/XMLParsing/Sample XML/CDs/CDCatalog.swift index 2dc7fd0..a195fc9 100644 --- a/XMLParsing/Sample XML/CDs/CDCatalog.swift +++ b/XMLParsing/Sample XML/CDs/CDCatalog.swift @@ -12,7 +12,7 @@ struct CDCatalog: Codable { var cds: [CD] enum CodingKeys: String, CodingKey { - case cds = "cds" + case cds = "CD" } } @@ -23,4 +23,13 @@ struct CD: Codable { var company: String var price: Double var year: Int + + enum CodingKeys: String, CodingKey { + case title = "TITLE" + case artist = "ARTIST" + case country = "COUNTRY" + case company = "COMPANY" + case price = "PRICE" + case year = "YEAR" + } } diff --git a/XMLParsing/Sample XML/Plants/PlantCatalog.swift b/XMLParsing/Sample XML/Plants/PlantCatalog.swift index 8cd369d..18d1559 100644 --- a/XMLParsing/Sample XML/Plants/PlantCatalog.swift +++ b/XMLParsing/Sample XML/Plants/PlantCatalog.swift @@ -12,25 +12,28 @@ struct PlantCatalog: Codable { var plants: [Plant] enum CodingKeys: String, CodingKey { - case plants = "plant" + case plants = "PLANT" } } struct Plant: Codable { var common: String var botanical: String - var zone: Int + var zone: String var light: String var price: Double var amountAvailable: Int enum CodingKeys: String, CodingKey { - case common, botanical, zone, light, price - - case amountAvailable = "availability" + case common = "COMMON" + case botanical = "BOTANICAL" + case zone = "ZONE" + case light = "LIGHT" + case price = "PRICE" + case amountAvailable = "AVAILABILITY" } - init(common: String, botanical: String, zone: Int, light: String, price: Double, amountAvailable: Int) { + init(common: String, botanical: String, zone: String, light: String, price: Double, amountAvailable: Int) { self.common = common self.botanical = botanical self.zone = zone @@ -50,7 +53,7 @@ struct Plant: Codable { let values = try decoder.container(keyedBy: CodingKeys.self) let common = try values.decode(String.self, forKey: .common) let botanical = try values.decode(String.self, forKey: .botanical) - let zone = try values.decode(Int.self, forKey: .zone) + let zone = try values.decode(String.self, forKey: .zone) let light = try values.decode(String.self, forKey: .light) let priceString = try values.decode(String.self, forKey: .price) let price = Plant.currencyFormatter.number(from: priceString)?.doubleValue ?? 0.0