All About Codable in Swift (Part 1)

  1. Why we need Codable (Background)
  2. What is Codable, Encodable and Decodable
  3. Decode plain json to Swift custom model and vice versa
  4. Coding Keys (Keys you don’t want to parse)
  5. Keys Exists in json but not in the Model
  6. Key in model can exists or not in your json
  7. Model and json with different structure keys (Decoding)
  8. Model and json with different structure keys (Encoding)
  9. Custom Decoding With Key may or maynot exists in Json
  10. Working with Type Mismatch
https://learnappmaking.com/codable-json-swift-how-to/

Background

Codable

Encodable Protocol

Decodable Protocol

Getting Started

  1. Created a Swift datatype compose object we called it custom data Type and we conform to Codable protocol
  2. Created JSONDecoder object facilitates the decoding of JSON into semantic types (type conforming to Decodable protocol). That’s why our . custom type conforming to Codable protocol. Codable is a type alias for the `Encodable` and `Decodable` protocols.
  3. decoder.decode converted json into custom type as shown in Figure 1.
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5

Keys you don’t want to parse

Figure 6
Figure 7

Keys Exists in json but not in the Model

Figure 8

Key in model can exists or not in your json

Figure 9
Figure 10

Model and json with different structure keys (Decoding)

  1. By writing your own decoder you can specify container in our case we first created container with CodingKeys and we only decode name,id and type from json Then we created CompanyCodingKeys and we expect companyName and companyId in json since it exists in enum compiler is happy
Figure 11
Figure 12

Rule of Thumb 1:

Figure 13

Model and json with different structure keys (Encoding)

Figure 14
Figure 15
Figure 16

Custom Decoding With Key may or maynot exists in Json

Figure 17
Figure 18
Figure 19
Figure 20
Figure 21

Working with Type Mismatch

Figure 22
Figure 23

Useful Links

--

--

--

Senior iOS Engineer | HungerStation | Delivery Hero

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lessons Learned from Losing a Rockstar Dev

CS373 SPRING 2022: John Mackie

Dev Status Update — June, 2020

Generating sequence number in Apache Hive

Photo by <a href=”https://unsplash.com/@beeing?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Beeing</a> on <a href=”https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

Let’s create the simplest weather app with flutter.

History Of Programming Languages. Educational — Logo

Oasis Infobyte Internship experience

AWS Architecture — Everything You Need to Know About It!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Akhtar

Ali Akhtar

Senior iOS Engineer | HungerStation | Delivery Hero

More from Medium

iOS-Introducing DispatchQueue in Swift

How to Implement Light and Dark Theme Using Xcode

iOS: Asset Catalogs, UIButton, CALayer & More…

Singleton Design Pattern in Swift

Macbook with some code on the screen