Swift 4 Decodable

Rushikesh Reddy
Sep 9, 2018 · 1 min read

Today we will discuss how to implement swift Decodable to parse JSON data from a server in a few simple steps without any third-party libraries.

Our URL: https://rss.itunes.apple.com/api/v1/in/ios-apps/top-free/all/10/explicit.json

There are 2 steps in implementing Decodable

  1. Create structs according to the JSON data, let's say we need 10 top free app names, app images from the app store to display in our app, create struct according to our JSON data. ( we took iTunes RSS feed link because our aim is to show how we can easily achieve parsing bit complex JSON data. {{[{name:String}]}} )
our struct

2. Parse JSON using decodable.

//1: Creating appUrl

//2: Instantiate URLSession

//3: Creating decodedData using JSONDecoder.decode(structName, from: data received from server)

//4: As JSONDecoder can throw errors we need to handle errors.

//5: resume() does resume the task.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade