title: Decoding optional values in JSON - Swift 4
body: <p>I've read read many tutorials and searched for answers here on SO;</p>
  
  <p><a href="https://stackoverflow.com/questions/52377458/decoding-nested-json-with-optional-values-swift-4">Decoding Nested JSON with Optional Values Swift 4</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/47566292/swift-4-not-decoding-json-optional-properly">Swift 4 Not decoding JSON optional properly</a> </p>
  
  <p>Decoding optionals doesn't seem problematic using below code. Obviously I'm doing something wrong, but what is it!?</p>
  
  <p>Part of my code:</p>
  
  <pre><code>struct ProductInfo: Codable {
      var name: String?
      let images: [Image]
      var ingredients: String?
      let origin: [Origin]
      let producer: Producer
  
      enum CodingKeys: String, CodingKey {
          case name = "Artikelbenamning"
          case images = "Bilder"
          case ingredients = "Ingrediensforteckning"
          case origin = "Tillverkningslander"
          case producer = "Varumarke"
      }
  
      init(from decoder: Decoder) throws {
  
          let values = try decoder.container(keyedBy: CodingKeys.self)
  
          name = try? values.decodeIfPresent(String.self, forKey: .name)!
          images = try values.decodeIfPresent([Image].self, forKey: .images)!
          ingredients = try? values.decodeIfPresent(String.self, forKey: .ingredients)!
          origin = try values.decodeIfPresent([Origin].self, forKey: .origin)!
          producer = try values.decodeIfPresent(Producer.self, forKey: .producer)!
      }
  }
  </code></pre>
  
  <p>The app queries a products database. Following json is returned when a product doesn't exist. App crashes when trying to decode nil name ("Artikelbenamning in json) in init(from:). </p>
  
  <pre><code>{
      "GTIN": null,
      "TillverkarensArtikelnummer": null,
      "Artikelbenamning": null,
      "RegleratProduktnamn": null,
      "Forvaringsinstruktion": null,
      "Variabelmattsindikator": false,
      "Bruttovikt": null,
      "Bredd": null,
      "Djup": null,
      "Hojd": null,
      "Returemballage": false,
      "FarligtGodsKod": null,
      "FarligtGodsKlass": null,
      "FarligtGodsForpackningsgrupp": null,
      "GPCKod": null,
      "GiltigFROM": null,
      "Publiceringsdatum": null,
      "FakturerbarEnhet": false,
      "Slutdatum": null,
      "GiltighetsdatumPris": null,
      "Tillganglighetstidpunkt": null,
      "SistaTillganglighetstidpunkt": null,
      "SkapadDatum": null,
      "SenastAndradDatum": null,
      "Flampunkt": null,
      "KodBegransadMangd": null,
      "OfficiellTransportbenamning": null,
      "OspecificeradTransportbenamning": null,
      "TunnelrestriktionADR": null,
      "KlassificeringskodFarligtgods": null,
      "Transportkategori": null,
      "Konsumentartikel": false,
      "BestallningsbarForpackning": false,
      "RabattOlaglig": null,
      "Garantiloptid": 0,
      "Konsumentdatum": null,
      "Tjanst": false,
      "Sasongsindikator": null,
      "Engangskop": null,
      "AntalReturnerbaraEnheter": 0,
      "Staplingsriktning": null,
      "Staplingstyp": null,
      "MaxTransportTemperatur": 0.0,
      "MinTransportTemperatur": 0.0,
      "Anvandningsinstruktioner": null,
      "HallbarhetEfterOppning": 0,
      "Riskfras": null,
      "KodlistutgivareRiskfras": null,
      "Klassificeringssystem": null,
      "FarligtGodsBegransadMangd": null,
      "FarligtGodsOvrigInfo": null,
      "FarligtGodsSarbestammelser": null,
      "T3495_Artikelavisering": null,
      "T4032_TypAvUtgangsdatum": null,
      "T3742_ForstaLeveransdatum": null,
      "Undervarumarke": null,
      "Niva": null,
      "Produktbladslank": null,
      "KompletterandeProduktklass": null,
      "T4200_AllmänPubliceringstidpunkt": null,
      "T3848_TypAvTryckkanslighet": null,
      "Varningsetiketter": [],
      "Sasongskoder": [],
      "Produktklasser": [],
      "MaskinellMarkningar": [],
      "Bilder": [],
      "ReferenserTillAndraArtiklar": [],
      "MSRKritierier": [],
      "Kravspecifikationer": [],
      "Receptlinks": [],
      "Allergener": [],
      "Markningar": [],
      "Ingredienser": [],
      "Tillagningsinformation": [],
      "Tillverkningslander": [],
      "Naringsinfo": [],
      "Serveringsforslag": [],
      "Diettyper": [],
      "Tillagningsmetoder": [],
      "Farger": [],
      "VillkorForsaljning": [],
      "Varumarke": {
          "Varumarke": null,
          "AgareGLN": null,
          "AgareNamn": null,
          "Tillverkare": {
              "Namn": null,
              "EAN": null
          }
      },
      "Nettoinnehall": [],
      "Kontakter": [],
      "Faroangivelser": [],
      "Sakerhet": [],
      "Forpackningar": [],
      "Tillsatser": [],
      "Substanser": [],
      "Fangstzoner": [],
      "Marknadsbudskap": null,
      "KortMarknadsbudskap": null,
      "Komponenter": null
  }
  </code></pre>