Decode JSON in Swift: A Full Micro-Project
A whole project?
--
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
The video
The content of this article is covered in the following video!
Prerequisites:
- This article skips over setting up a project, and gets rid of the storyboard and uses programmatic constraints as in this article
- Later in the article a real example is used, that is Decoding JSON and the internal workings of this are kind of skipped over
- If you are interested in testing, or a more in-depth explanation of JSON decoding you can look at this article
Keywords and Terminology:
JSON: JavaScript Object Notation, a lightweight format for storing and transporting data Swift: An open source programming language for macOS, iOS, watchOS and tvOS
The project implementation
The expected result
This Project is going to take data from an endpoint (the specific endpoint is one of those hosted at reqres) and displays the result in a UITextView
.
The end project looks something like the following:
So let us press on!
I’ve used [weak self] (the difference between weak and unowned is explained here )
The detail
Users Model The model is defined by the structure of the data from the endpoint. https://reqres.in/api/users?page=2 is
{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"tobias.funke@reqres…