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
The final type we want to model is a Song so that we can show the user what an album detail page looks like. In this video, let's use the lookup endpoint again to get song information.
-
0:00
To get information about songs in an album,
-
0:03
we're going to use the look up end point again.
-
0:06
The only difference this time around is instead of the album entity,
-
0:10
we're going to specify song.
-
0:12
So back in Postman, we'll copy the base URL, so from here we're
-
0:17
gonna get everything up to lookup, again, and in a new tab we'll paste this in.
-
0:23
And like with the album, the way we're gonna search for
-
0:26
a song is to provide an ID to lookup.
-
0:29
Instead of an artist ID, we're going to the ID of an album,
-
0:33
which back in our response here, if I hit Send to get that again.
-
0:37
And then load this into JSON.
-
0:40
So if we look here, we have a collection which is an album, and
-
0:43
it has a collection ID.
-
0:45
So let's copy this and grab the collection ID for the first album.
-
0:50
And then back in our final request in our lookup, we're going to specify here,
-
0:55
under params, an ID.
-
0:58
And we'll paste in that value and then for the entity we'll say song.
-
1:02
Done and done, okay let's hit Send.
-
1:05
Again we'll have to switch this to JSON.
-
1:08
Just like before we have a results array with nest to dictionaries.
-
1:13
And like the album queries started off with a dictionary that contained
-
1:17
information about the artist.
-
1:19
The first item in the results array for
-
1:22
the song query is a dictionary containing information about the album.
-
1:27
Now, we're not going to use any of these data though because on the final detail
-
1:30
page, we're going to use the album class as our model and
-
1:34
we already have this information.
-
1:35
So what are we gonna get?
-
1:37
Let's scroll down to the next section.
-
1:39
Here we have a dictionary that represents a song.
-
1:42
So the wrapper type is track and the kind is song.
-
1:45
And there is a lot of information here.
-
1:48
We're going to keep it simple and we'll use the track ID,
-
1:51
the track name, we'll also grab the tracks censored name.
-
1:56
We'll get the tracked time down here which is in milliseconds.
-
2:00
And then finally, whether the track is explicit or not.
-
2:03
If you want any of the other info, go for it.
-
2:06
So now let's jump back to Xcode and in the Song struct we created earlier,
-
2:11
let's add these relevant stored properties.
-
2:14
So id, type Int, name, String.
-
2:20
censoredName also a string, let trackTime, this is an Int.
-
2:26
And finally whether the the track is explicit or not, and that's a Bool.
-
2:32
Now one thing I want to point out are types.
-
2:34
So Album, Song, and Artist all have stored properties that have the same name.
-
2:39
So we have an id here, we have a name here.
-
2:41
And in out JSON response these have different names.
-
2:43
So collection id, artist id.
-
2:46
The reason we're not using those names in here is because
-
2:49
the type provides some name spacing.
-
2:52
So when I refer to this property it'll be song.name or artist.name or album.name.
-
2:58
So there is already that meaning there and
-
3:00
I don't have to get some redundant information in the stored property name.
-
3:03
We don't want album.ablumname for example.
-
3:06
Now since this is a struck, we get the member wise initializer, so
-
3:09
we don't need to type on out manually.
-
3:12
And with that, we have all our models.
-
3:15
So now is the interesting bit,
-
3:16
let's use these models to create some faked data to use as a stub in our app.
-
3:22
What's a stub?
-
3:24
Well, in this context,
-
3:25
it is some code that we can use as a stand in for actual functionality.
-
3:29
Eventually, we want data from the web and to use that data in our UI.
-
3:34
But just for the sake of setting up our UI,
-
3:37
getting real data involves a fair bit of work.
-
3:39
We need to model our endpoints.
-
3:41
We need to write networking code.
-
3:42
We need to parse the results and so on.
-
3:44
By using fake data we can immediately start building the UI and
-
3:48
getting something barebones up on the screen.
-
3:51
We won't run into any errors with the data because it is a placeholder that we
-
3:55
define.
-
3:55
And then once we're done with the UI we can circle back and
-
3:58
figure out how to get real data.
-
4:00
Which is a separate problem on its own with its own set of complexities.
-
4:04
So let's add a final file to this model's group.
-
4:09
We'll call this Stub.
-
4:11
And in here, we're going to model a fake response in exactly the same way we
-
4:15
expect to get back from the web.
-
4:18
So we'll start with the struct and we're doing this just so
-
4:20
that we have some name spacing again.
-
4:23
And the stub is going to be a simple struct with a series of computed
-
4:26
properties.
-
4:27
For example, we'll start with artist, static var artist of type artist.
-
4:34
Now, in this property, let's return an instance of artist populated with data
-
4:39
that we can grab from the JSON response in Postman for it looking up an Artist term.
-
4:44
So we'll say a return Artist, and then you can go over
-
4:50
to Postman, and in this first response here where we look up an Artist,
-
4:55
let's grab the JSON, and here we'll grab some data from the first result.
-
4:59
So, we want an artistId, so we'll copy that, and
-
5:04
the rest of the stuff I can just type out myself.
-
5:06
So I'll paste this in, the name is Taylor Swift.
-
5:13
PrimaryGenre is pop, and the albums, well, an empty array for now.
-
5:19
When we search for
-
5:20
an artist term, even though our model contains a stored property for
-
5:23
the albums, the actual JSON response doesn't have any of that information.
-
5:27
We already know that.
-
5:28
To get this, we need to make that second lookup call.
-
5:31
So here, we're just going to return an empty array for the albums.
-
5:34
And this is what I mean in that our stub will model the exact same response
-
5:38
as our web request.
-
5:40
When we get an artist for the first time, albums will be empty so
-
5:43
we need to do some additional work to get the album value.
-
5:46
Okay, so that's it for artist and next is an array of albums.
-
5:50
Again, this is a bunch of copy pasting, so you can either do it on your own,
-
5:54
go back and forth between Postman and get the relevent data, or
-
5:57
just ground the code from the snippet in the teachers notes.
-
6:00
That snippet includes all of the stub data, so
-
6:03
you can just simply paste it all in.
-
6:05
Let's take a break here.
-
6:06
Over the next set of videos we'll get our entire UI up and
-
6:09
running to display all of this fake data.
You need to sign up for Treehouse in order to download course files.
Sign up