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
Using the URL we just constructed, let’s make a network request (the wrong way!) using the Data type.
Teacher’s Notes
-
0:00
Now that we have a URL let's figure out how we can request data
-
0:03
from the Dark Sky API.
-
0:04
To do this we're going to use the data type.
-
0:08
Now before we do anything let me be perfectly honest with you.
-
0:12
What we're about to do is the wrong way of doing things.
-
0:15
Now the code won't be wrong in that it won't work, it certainly will.
-
0:20
But we're going to be committing quite a faults by doing it this way.
-
0:23
The reason I'm going to do it this way is to show you the pitfalls of
-
0:27
this approach and to introduce a couple of crucial topics.
-
0:31
Understanding what makes this approach wrong, for
-
0:33
lack of better word, will make for a much better explanation.
-
0:37
So rather than just telling you, let's walk through it together.
-
0:40
I've also run into this kind of code here and there.
-
0:43
And I want to show you why you shouldn't write it this way,
-
0:45
if you see it on a blog post somewhere.
-
0:48
Now the data type is an important one, and
-
0:51
provides a way to represent data in our code.
-
0:53
And by data, I mean bits and bytes.
-
0:56
Using the data type, which is a struct, we can create an object that helps
-
1:01
us manage and store the data we get back from ranking a network call.
-
1:05
Fetching data from a URL, whether it's pointing to a file on disk or
-
1:10
a resource on a server, and
-
1:12
storing the resulting information in a data object is quite a common task.
-
1:17
And the data type has a convenient initializer, contentsOf, that takes a URL.
-
1:22
The method returns an instance of data with the contents of whatever was at
-
1:27
that URL.
-
1:28
So now that we have a URL, let's create an instance of data using this initializer
-
1:33
to see if we can actually get the data that's at the end of our forecast URL.
-
1:38
So let weatherData = try!, with a forced exclamation,
-
1:44
contentsof, and here we'll say forecasturl.
-
1:50
Now this is optional, so I'm just gonna go ahead and enforce rabbit for now.
-
1:53
This is a throwing method, because as you undoubtedly know with trying to load
-
1:58
a URL, many things can go wrong.
-
2:00
because we're eventually going to get rid of this code,
-
2:03
we're just going to use a force try to avoid error handling logic.
-
2:06
And again, notice we had to force unwrap the URL here as well.
-
2:10
This is because the initializers we used to create the URL, both the string and
-
2:15
string relative to base were fillable initializers.
-
2:18
URL construction can fail for various reasons like passing in illegal countries.
-
2:24
We will go into all this in depth in a future course.
-
2:27
When we run the app,
-
2:28
we are going to automatically make a network call with the URL we've
-
2:32
provided and assign the resulting data object to this constant weather data.
-
2:37
If something goes wrong because we're using a forced try, our code should crash.
-
2:43
So right after we attempt this, let's print the contents of the instance.
-
2:47
Now if this works,
-
2:48
this print statement should show us some information about the instance.
-
2:51
So we'll say print(weatherData), okay?
-
2:56
I'm going to run.
-
2:58
Now once you run your app, the console should pop up with
-
3:01
information about the size of the data object, as you can see here.
-
3:06
Okay, so we didn't crash.
-
3:08
Yay us.
-
3:10
The code works because this forced try that we're using here didn't fail.
-
3:14
While we can't see the contents of this network request, the fact that
-
3:18
the resulting object has a size means we did get some information back.
-
3:23
So we managed to get some information by actually hitting this URL, but
-
3:27
it doesn't look like the data we saw in the Dark Sky documentation earlier,
-
3:31
which I mentioned was that structured format called JSON.
-
3:35
but we do have a way of knowing if our API query was executed correctly.
-
3:40
So we can head over to the Dark Sky console.
-
3:42
If you're on the Dark Sky docks page, click Console at the top and scroll down.
-
3:49
And you can see that if you did this correctly,
-
3:51
you'll see that the number of calls you've made today has gone up.
-
3:55
Mine says two, I read it more than once.
-
3:57
But if you just read it once, it should say once.
-
4:00
Over many times you ran the app and made a call.
-
4:02
If it still says that you have zero total calls today, or this month, or whenever,
-
4:07
that means that you're having some issues with your code, so double check it.
-
4:12
And JSON to go back to it is a common data interchange format that we're going to
-
4:16
work with a lot in any app that talks with the web server.
-
4:20
Before we get into working with it however, let's understand what JSON is and
-
4:24
in particular, what the JSON we're going to be working with looks like.
You need to sign up for Treehouse in order to download course files.
Sign up