Part 1
Decoding JSON Data

We continue where we left off last time and dive into writing our first lines of code. The core of any app is the app logic, and a big part of the app logic is in the data models. What kind of data we will work with internally? How will that data be stored in memory? In this video, we will write our data models, make them Codable (and we learn what that means), and create a global public function that will read and decode our three JSON files we prepared last time, in order to populate our newly created data models with real data.

Outline

  • writing a struct

  • let versus var

  • what is the Codable protocol and how to make a custom structure conform to Codable

  • what is an optional type

  • what is a static function

  • how to guard against particular conditions

  • how to deal with functions that throw (an error)

    • the do-try-catch code syntax mechanism

  • provided .debugDescription computed property on optional types

  • data types/structures available in the Foundation framework:

    • URL

    • Data

    • JSONDecoder

    • Error

  • how to decode JSON data into a custom structure

    • how to locate a resource file in the app bundle with Bundle.main.url(forResource:withExtension:)

    • how to read the contents of an URL into a Data object with the initializer Data(contentsOf:)

    • how to decode a Data object into a custom structure with JSONDecoder(_,from:)