Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Parsing JSON Data

Parsing JSON data is one of the most fundamental skills to learn as an iOS Developer. Most apps make a network request to get data from a remote server, and once that request is executed, you’ll need to convert that data into a Swift object that can be used within your app.

Handling Errors Gracefully

When you’re making networking requests and parsing JSON data, there’ll be plenty of opportunities for error. The errors can show up in many forms. The two most common categories are errors related to networking, and errors associated with decoding or parsing. To cover these scenarios, you’ll implement a NetworkError enum that contains the error cases you want to cover. These cases are somewhat arbitrary based on the scenarios the code naturally leads into whenever you hit an else or error block. Navigate to the NetworkError file and replace NetworkError with the following code:

Connecting Parsed Data to UI

Up to this point, you’ve parsed the necessary models and handled the potential errors that could come up. You’re finally able to show this data via the UI. Before you update the ContentView, you’ll implement the WeatherViewModel, which will be your layer updating the user interface. Navigate to the WeatherViewModel file and add the following properties and constructor:

See forum comments
Download course materials from Github
Previous: Introduction Next: Conclusion