Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Codable is so easy to use we can parse some JSON into a model with less than 5 lines of code. Let's check it out!
Documentation
Code Snippets
-
0:00
[MUSIC]
-
0:00
Hi, my name is Pasan, and
-
0:05
I'm an iOS Instructor at Treehouse.
-
0:13
In this series, we're going to take a look at serialization and
-
0:16
deserialization, or the encoding and decoding of data in Swift.
-
0:22
Much of what we do as Swift developers, whether it's moving iOS apps or
-
0:26
writing server-side code, is taking data that's constructed in a specific format,
-
0:31
say, JSON or a PLIST, and converting that to Swift objects.
-
0:36
In the past,
-
0:36
we've achieved this by leaning on foundation types like NSJSONSerialization,
-
0:41
which took in some JSON data and returned a foundation object.
-
0:45
While this got the job done, it wasn't ideal.
-
0:48
The class was not built with Swift in mind.
-
0:51
We would still use error prone methods,
-
0:53
like using stringly typed keys to parse the resulting object.
-
0:57
We had optionally cast inspect values, and we had
-
1:00
to coelest several points of failure, something coherent at the call site.
-
1:04
None of this felt at home in the type-safe world of Swift.
-
1:08
And this was evident by the fact that there was an explosion of third-party
-
1:12
JSON parsing libraries, each with their own set of custom operators.
-
1:17
In Swift 4 the core team added a language feature to handle this.
-
1:20
And that is what we are here to talk about.
-
1:23
So without any further ado, let's dive right into the Codable protocol.
-
1:28
Swift's attempt at solving this problem is by means of a protocol, or
-
1:32
rather a set of protocols.
-
1:34
So let's see how this works.
-
1:36
I have here a playground filed with some starter code.
-
1:39
And I'd like you to go to the notes section of this video, and
-
1:41
grab the same file using the download link provided.
-
1:44
Now, if you already went ahead and created your own playground to begin with, and
-
1:48
you just want to get the code and paste it in, there are links for that as well.
-
1:53
So in the playground,
-
1:53
make sure you're starting on this playground page named A Quick Peek.
-
1:58
There may be more than one playground page.
-
2:00
So if you have one of those files, just go to the navigator area by clicking this
-
2:04
icon, and then navigating to A Quick Peek.
-
2:07
So in here we have some simple sample JSON to start with,
-
2:11
defined as a multi-line string literal.
-
2:14
This isn't actually how JSON would be transmitted in a networking response, so
-
2:19
right after we have some code that's converting the string literal to a data
-
2:24
object using the UTF-8 string encoding.
-
2:27
This JSON represents some information about a particular employee
-
2:31
in some database.
-
2:32
Now, in a code base, we won't be working with string or data instances.
-
2:37
And instead we would have a model that represents this.
-
2:39
So we have one right here.
-
2:42
Now, our goal is to get the information, encapsulate it in this JSON response,
-
2:47
which is currently represented as an instance of the data type,
-
2:52
as an instant of the model.
-
2:54
And we can do this in three easy steps.
-
2:58
So step one, we're going to make our model conform to the Codable protocol.
-
3:04
There should be no additional work to do beyond that, and
-
3:07
we'll get into the why later.
-
3:08
Now, step two, and that's all we need to do with the model.
-
3:12
Step two, we need a decoder to handle decoding the JSON, parsing it,
-
3:17
and assigning values to the stored properties in our model.
-
3:21
Foundation now includes, as a Swift 4, new built-in decoder types.
-
3:25
So let's create an instance of JSON decoder.
-
3:31
Now that we have a decoder,
-
3:33
we can ask it to give us an instance of our model using the JSON provided.
-
3:39
So I'll say, let employee = try decoder.decode.Employee.self, from: json.
-
3:48
And just like that, we have an employee instance.
-
3:55
There are two important things to note here.
-
3:58
One is that this is a throwing operation.
-
4:01
For now we're ignoring this by using the forced version of the try.
-
4:06
Second, when we decode, we simply indicate what type we want to decode to.
-
4:10
So over here the type I want to decode to is employee, but
-
4:14
I don't pass in an instance because that's what we want.
-
4:17
Instead I pass in the type by saying Employee.self.
-
4:21
And that's all we need to do.
-
4:22
So in the results area, you can see that we have a valid employee instance.
-
4:26
And we can also inspect properties on the return instance.
-
4:30
So we can say employee.name.
-
4:32
Let me switch that, this is lowercase.
-
4:35
And you'll see that in the results area,
-
4:38
this matches up to the JSON data that we had defined earlier.
-
4:42
So let me scroll up a bit, so it says Employee name Pasan, id 1,
-
4:45
and that's what we have here.
-
4:47
Pretty easy?
-
4:49
Of course, there is a good amount of magic going on here.
-
4:52
So over the next few videos, let's break this simple example down.
You need to sign up for Treehouse in order to download course files.
Sign up