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
Before we write any code to fetch data, let's explore the service we're going to use, read the documentation and define what our model will look like.
-
0:00
The service we're going to be using for this app is called Dark Sky,
-
0:05
and can be found at the website darksky.net/dev.
-
0:09
This service is called an API, something that will define very soon.
-
0:13
To use this service you need to register.
-
0:16
But we're going to hold off from doing that just yet.
-
0:19
We're here to read the documentation so
-
0:21
that we can see how this service provides data.
-
0:24
What we're going to do with this project is create our data models first.
-
0:28
And set up the app to make sure that everything works with some fake hard
-
0:32
coded data.
-
0:33
Why are we doing this?
-
0:34
Well in a previous course we learned that it is good design to
-
0:37
separate the data model from the source of the data.
-
0:40
We want the model we're going to design to display some basic
-
0:43
information about the weather.
-
0:45
We can design the model independent of the data source, and
-
0:48
then configure our UI to use the model to set itself up.
-
0:52
This way when we implement the data source the model doesn't care where the data
-
0:56
comes from just that it receives it in a particular format.
-
1:00
Also by verifying that the rest of the app works as intended first,
-
1:04
we know that if something goes wrong later on it is most likely our networking code.
-
1:09
We've talked about the MBC patter before.
-
1:12
Now in our starter files, we already have the view setup and
-
1:15
there's a controller provided as well.
-
1:17
Once we have the model in place and loaded with some fake data,
-
1:21
that's actually enough to write the logic to configure the views to display it.
-
1:25
Now from there changing out the source of the data shouldn't affect how
-
1:28
the rest of the app works.
-
1:30
Okay.
-
1:30
So back to the Dark Sky documentation.
-
1:33
Up at the top there should be a link to the docs.
-
1:35
And if we had over there you can click through to a section labeled forecast
-
1:40
request.
-
1:41
So down here under weather API call types you will see a link written forecast
-
1:45
request.
-
1:46
Here its says that if we make a request, we'll talk what request are.
-
1:50
But if we make a networking request using this URL structure
-
1:54
we get a bunch of data back that looks like this.
-
1:58
Don't worry about how all of that will happen, I'll explain it in due time.
-
2:02
Now if you look at the data, there's a lot of it here.
-
2:05
But since our app is going to display the current weather,
-
2:07
I'm mostly concerned about the data in this currently segment.
-
2:11
And if you look at this data inside of currently you'll see that there's some
-
2:15
very specific names for values, time, summary, icon, and so on.
-
2:20
To figure out what each of these mean, and the type of data they return
-
2:24
we can head over in the link section here, head over to the response format section.
-
2:30
Okay, that didn't work.
-
2:31
Let me click it again.
-
2:33
Let's go back to the docs.
-
2:35
And this is just actually all the way at the bottom.
-
2:38
So I'm gonna scroll down since that link doesn't seem to work.
-
2:41
And here we go, Response Format.
-
2:46
So in this section you'll see that you have information about each of those data
-
2:50
points provided.
-
2:51
Scroll around, and you'll see that the information we care about, the data
-
2:56
that we're modeling is represented by the currently type, currently key.
-
3:01
Now if you click through to this data point thing again, and go right down.
-
3:05
You'll see that there is actually tons more data that we get back by requesting
-
3:09
the current weather.
-
3:11
We don't really care about all of these things, so
-
3:13
if you inspect them further you'll see that some of these things like Sunrise and
-
3:17
Sunset for example are only relevant if you are looking at the daily weather.
-
3:23
Others apparent temperature, it is not there on the daily weather information.
-
3:28
So when we look at the current weather what do we really care about?
-
3:31
Well we care about the temperature mostly,
-
3:33
which is down on this list, right at the bottom.
-
3:35
This is ordered alphabetically so temperature is right here.
-
3:40
We'd also like to know the humidity level, maybe the chance of rain,
-
3:43
a text summary, and finally we'd like to show our users an icon.
-
3:47
So all of that is available under this currently structure,
-
3:50
that should be good enough.
-
3:52
So using this information let's build our model.
-
3:55
Let's add an empty Swift file.
-
3:57
So if we go back to our projects, we'll add an empty Swift file, and
-
4:02
we'll call it current weather.
-
4:07
We're going to use a struct as our data model.
-
4:11
Containing the few properties we established as necessary
-
4:14
to display the current weather.
-
4:16
So struct CurrentWeather.
-
4:21
First thing we need to store the temperature.
-
4:23
Now if we go back and look at the documentation under these data points.
-
4:26
So go to Response Format, then these data points under currently.
-
4:30
On each of these it says that these objects may contain
-
4:33
the following information.
-
4:35
Because as you can see a lot of these properties are optional.
-
4:39
The type of information we're looking for
-
4:41
is pretty specific to the type of requests that we're making.
-
4:45
So on temperature here for example, you'll see that it is an optional property,
-
4:49
but it's not a defined on minutely.
-
4:52
Which means that we can guarantee that it is defined on the currently information,
-
4:56
which is what we want.
-
4:58
Now since some of these data points may not be available for
-
5:00
the type of request we're making, we may need to use optionals.
-
5:04
So if you are modeling the minute to minute weather,
-
5:07
then temperature will be an optional property.
-
5:10
Because it's guaranteed to be available for
-
5:12
current weather we're going to model it as a non optional.
-
5:14
So then going back we'll say let temperature of time double.
-
5:20
Next step is humidity.
-
5:22
There are no notes about humidity not being available for the CurrentWeather.
-
5:25
So again let humidity, and this is going to be a non optional Double.
-
5:32
After humidity, we'd like to let users know if it's going to rain.
-
5:36
Back on the list this is listed as Precip Probability.
-
5:40
Again there doesn't seem to be any notes about it not being available for
-
5:43
a particular type.
-
5:44
So we can be confident, somewhat confident that we don't need an optional.
-
5:48
So let's say let precipProbability, and this is of type Double.
-
5:55
Next up is the text summary of type String.
-
5:59
And finally we'd like to show an icon.
-
6:02
Now let's go back to the docs for a second, and navigate to icon right here.
-
6:09
According to the docs the icon property returns a string
-
6:13
describing a type of icon to show.
-
6:16
At the start of files for
-
6:17
the project already include a set of icons in the assets folder.
-
6:21
So it's just the matter of matching an icon based on the streams values that you
-
6:25
see here, that this property returns.
-
6:28
So back in the CurrentWeather struct let's define an icon as a string, and
-
6:33
just store the name of the icon, okay?
-
6:35
And that should be if for a current weather object, quite simple.
You need to sign up for Treehouse in order to download course files.
Sign up