Hey everybody what's going on this is Caleb with slopes dot com.
And in this video we're going to download that additional data that we need.
The contributor's data as well as the avatar image and we're also going to set up some conditional safety
checks to make sure that all the repositories that we are downloading have all the properties we need.
If you remember in the last video we got a crash because one of the Repos did not have an avatar you
or else so we're going to set it up so that when we download that data we're only going to get data
that has all the properties we need and we can actually checked against that using guard Blatt which
is really really really cool.
So go ahead and pull open your X code project just like so and we're going to do that.
Now we're going to make sure that everything is coming in the way we want.
OK.
So inside your trending repots dict array function we're going to go into the for loop here and we're
going to go inside our if trending repots array count is less than or equal to 9.
And this is where we're going to use guard let to ensure that every repo has the values we need.
And we're going to use guard guard led to do that.
So go ahead and type guard let And we're going to create an individual property for all of these.
Remember these are all of the properties that we want.
So I'm actually going to go ahead and unstick this because after we fix this we will no longer have
a problem with these values coming in.
So go ahead and copy this.
OK we're going to use it up here and you're probably saying oh that's copying and pasting.
Yeah that's true.
But down here we're basically casting them as values in swift and then we're using them to create an
instance of repo so it's different.
But now what we can do is we can color guard let And the cool thing with a guard that is we can use
a string and not a string but a we can use.
OK.
A long list of properties and we can put them into the same guard let so that we make sure that all
of the values come back.
Otherwise we're just going to return.
So it's basically going to allow us to go through fill all the values.
If there's a value for all of it it will create all of them if one is missing.
It doesn't create any of them so it's a really cool safety check.
But we need to basically put commas between them and that will help us to separate them.
And let's do that now so we're going to go ahead.
Last there for string.
Let's go ahead and put a comma and a space and one more comment in a space.
OK.
Very cool.
And after each comma we can actually go in and return these down.
So it looks a little prettier.
Right before the let.
OK.
Very cool.
So it looks like we're getting avatar you are l a name description number of fork's language repo you
RL.
OK we have one more that we need.
We need that contributor's data.
So go ahead and put a comma.
Let contributor's you RL And that's going to be equal to the dictionary.
But then the contributor's you are a let's go find out what the key for that is.
All right.
So contributor's you r l I believe is just contributor's underscore u r l Paiste that n it's going to
be of type string.
K then do we have any other ones.
We have a name description language for X repo contributor's data avatar you Arel.
Hmm let's think
well you know what actually I do notice something here.
The avatar you are l is not a part of the big dictionary that we're looking at.
It's part of another dictionary inside called owner.
So what we can do is we can basically cast owner and then pull out the avatar you are l.
We should do that to make sure that we don't miss something so I'm actually going to remove avatar you
are l and move this guard let name property appear then below contributor's you or else I'm going to
go ahead and paste that.
But we need to first cast the owner dictionary from our our function here so to do that.
Go ahead and beneath this just call Let owner dict equals dictionary.
Whoops sorry dict.
And that property is called owner.
Right.
But we need to cast this as a dictionary of string.
And any.
OK.
Oh and one other thing you notice all these errors.
Comma there to separate them.
You'll notice we get a ton of errors and that's because inside of a guard light you're not allowed to
force on wrap things you need to optionally cast them.
So we need to actually change these all to optionals because basically a guard let works under the premise
of these values are optional meaning if there is a value we're going to move forward if there's not
a value we're going to return.
So we need to make them all optional.
And it looks like it's saying use of an resolved identifier dict and I don't know why that is.
Oh that's because it's repo dict.
My bad.
I missed that the whole time that's why you don't copy and paste code folks.
OK so we're going to copy and paste it like I'm doing here sorry being a bad example.
Anyway so.
OK.
Check it out.
We now have a property for repo dict.
We have a property for all of these but remember owner DECT is now its own dictionary and the avatar
you are l is inside of that.
So we need to call avatar you are ELF from owner dict like so.
All right.
Now the interesting thing is since these are all connected with comma's we can call it else.
And if all of these values do not come in if we miss even a single one we are going to go ahead and
call break.
And that means it's going to break out of the for loop or at least break out of this section of the
for loop.
OK that's pretty cool.
So now assuming we've made it through and we have a value for every single one of these We're going
to go ahead and create a dictionary.
OK.
And that's the dictionary that we actually should be appending to are trending repose.
So let's go ahead and let's create Ribault dictionary.
OK.
Now you know what we should set an explicit type for this so that it knows that it is a dictionary of
type string.
And any.
And now let's go ahead and let's build it.
So the first key is going to be name right.
That's great.
And the name is going to come from the name property that we've already set.
Next we're going to go ahead and just call description.
And that's going to be from description.
Next we're going to have a number of forks.
Whoops that needs to be a key fork's underscore count.
I'm going to use the same ones as the repository so that we don't have any issues and that's going to
be set to be equal to number of forks.
Next we're going to have language.
And that's going to come from language.
All right what's next.
We have a few more we have let's see to do repo you RL.
Whoops sorry.
That's actually HVM l u r l underscore T.M. u r l and that comes from Ribault u r l.
Then we have contributors you are l so let's go ahead and let's cast that contributor's underscore you
are l.
That comes from the property contributor's you are l.
Then we have Avatar u r l and that comes from Avatar you are l.
All right so there our dictionary that has all the values we need right.
We have our contributors you are l we have our name description number of fork's programming language
Ribault you r l contributor's you are l and Avatar your L we don't need owners dictionary because we
just need the value of Avatar you r l so that looks pretty good.
And now we can just go ahead and pass Ribault dictionary and we can pass that back to our function and
this should work the same exact way.
Now when we go through here we will definitely have an avatar u r l which is pretty neat and we will
definitely have a contributor's you r l so you know what we can actually add in a property here for
that.
Let's just type let contributor's u r l equals dictionary and the value coming in is contribute towards
underscore u r l as a string.
Of course because it will be coming in as non-optional.
And now we are good to go we can use these two to download the image and download the contributor's
data.
So let's go write those functions now.
So go ahead and write above.
Let's see.
Well why don't we do this down at the bottom.
We're going to go ahead and go below here and I'm going to type phunk download image for and it's going
to be for a specific avatar you are l of type string.
String string.
There we go.
And since it's really dependent since the order is very important we're going to use a completion handler
so that each repository cannot move on until the function is finished in a very particular way.
So go ahead and cut type completion and it's going to be escaping so we can pass the image back to where
we want then we're going to create a function that returns an empty function and inside of this we're
going to return an image and that's going to be wups of type II image.
OK that's pretty cool.
So give it some curly brackets.
And now what we're going to do is we're going to use Alamo fire to pass in the avatar you Arel and return
an image.
So go ahead and call Alamo fire.
Request and pass in the avatar you are l k.
That's easy.
Next we're going to go ahead and call daat response.
Whoops can't type response response and you'll notice there is no particular way to return an image.
But that is where Alamo fire image comes in.
So go ahead and import that Alamo fire image.
Very cool.
And now watch what happens when we call response.
You'll notice there is daat response.
Image K and that that's going to return an image for us which is very cool.
So on the completion händler go ahead and press enter and the image response is going to be of type.
Data response so go ahead and type image's response.
K and we're going to pull out the value inside that data response of type image.
So for the image just in case we're going to be safe and use a guard lead and call image.
We're going to set it to be equal to image response and watch what happens when I type a dot.
We can pull out the result right here which you'll see the result casts an image inside.
So to pull out the value is to pull out an image.
Very cool so that's great.
But this is a guard so we need to say if there is not an image.
We're going to just return k.
We don't want anything to happen or for us to have a crash so it can return nil which is really cool.
So then what we're going to do is we're going to call our completion handler once it's downloaded we
can pass the image like so to our completion handler and now it's done very cool.
So this function will download an image for us from our avatar you are l.
Next we need to get our contributors data and we're going to do this in a very similar way.
We're going to call phunk download contribute tips and tribute Tyers data for contributors.
You are l of type string.
And for this we're also going to call a completion handler of type escaping and we're going to return
an empty function.
We're going to basically need a count of contributors so we're just going to use an underscore there
since we're not allowed to use named types and we're going to just say contributors of type int.
Right because that comes in as an integer value.
So.
Great let's think what do we need.
We have a few RL contributors you know we need some curly brackets of course.
OK so now let's use Alamo fire to basically pass in that you r l and see what happens.
So go ahead and type.
Alamo fire dot request wups Riku.
And pass in contributor's you are l.
Now we have not yet seen what happens when we call it contributor's you r l so let's be smart let's
go find that u r l right here.
And let's go ahead and paste pasted in.
And let's check it out.
So yeah.
When we load it up you can see this is the list of all of the contributors by username that have contributed
to swift which is pretty cool.
You can see them all here.
But as you can see they come in as well as an array of dictionaries so we can basically count the amount
of dictionaries and that'll tell us the amount of contributors.
There are two swift which is pretty cool.
So we know that we can just call the count.
But the cool thing is we know now that it is an array of dictionaries.
That's going to help us avoid crashes.
So let's go ahead and let's call Alamo Firedrake quest and we're going to get the response in Jaison
just like you might have expected.
And let's go ahead and press enter on the completion handler and call this the response which is the
Jaison response.
Let's be safe and let's use guard let Jaison and just like before we're going to use response.
Got results.
Results don't value.
And we're going to cast that just like we saw as an array of dictionaries and we don't know always.
We know all the keys are strings but we do not know that the type will not be any.
So we're going to be safe and just say string and any otherwise we're going to just go ahead and return
nothing perfect and we'll just quit out.
But we should get some day sun and let's just make sure that there is.
I mean just because there's a contributor's you are l doesn't always mean that there are contributors.
So if our Jason that comes in we can call Dohm is empty.
OK.
And if it's empty then we're going to go ahead and do nothing but we're going to do the opposite.
So if the opposite of Jason is empty meaning if there is Jaison what we're going to do is we're going
to go ahead and just create a contributions value and just basically say let contributions equal Jason
count.
All right so that's going to be the amount of items in the dictionary will tell us how many contributors
then we can call our completion and pass in contributors or can contributions I guess.
So now that function is finished now we need to string them together and set it up so that we can properly
create these repositories that we need.
So let's go ahead and head back up to our trending repo function.
And here we go.
So when we create this repository we're passing it in a temporary image and a temporary contributor's
you r l so we need to call it beforehand.
So let's go ahead and call download image for Avatar you Aurel pass in the avatar you are ill and upon
completion we have a returned image.
OK then we're going to go ahead and call our download contributor's data.
But now that we're inside a closure we need to call self download contributor's data for contributors.
You r l pass in that value.
Then upon completion we get returned contributions.
So now we have access to both of these returned image and return contributions.
So we can go ahead and take this repo cut it and pasted in here.
And for the image I can set returned image.
Oops I missed my comma there.
And for the contributors I can set.
I guess it's returned contributions.
There we go.
Returned contributions.
Very very cool.
So and then at the very end what I can do is I can return that repo.
But you know what you're going to notice there's a problem.
Unexpected non void return value in void function.
That's because when we're using closures you cannot return out of a closure you get trapped and that's
why it's super important to use a completion handler with an escaping closure.
So get rid of the return.
And what we're going to do is we're going to go ahead and call completion make it escaping.
We're going to.
Whoops.
Looks like I missed that.
We're going to pass in a function return an empty function.
And remember underscore for the property and what we're going to return this function is just a repo
right just like we were returning.
But we need to just type Ribault of type Ribault and we create one here so we can call our completion
handler in the same place and pass in repo.
So now as we go through we'll get all the values we'll download the image contributor's data and return
the completed repo.
That's exactly exactly what we want.
So it looks like we are getting an issue here.
It's saying we're missing our parameter here so I'm going to actually rewrite the function to show you
what it should look like.
We're going to call self-direct download trending repo K from dictionary and that is dict as we passed
in from our for loop and the completion handler.
We're going to get a returned repo.
Right.
And that is where we can append our returned repo to our returned repots array.
But we're not passing in the proper repo.
So we're going to go ahead and call download trending repo but you'll notice it's all exed out.
And that's because we are setting it as a property which we needed to before but now we don't.
So go ahead and just pass in the dictionary and on completion we're going to just get a returned repo.
Remember right here in our function we pass in the repo to the completion handler once we have the image.
Once we have the contributor's data and now inside here we can append our returned repo.
So instead of returning that repo return the returned repo.
OK.
So that will go through.
That will all do its thing.
And then at the very end once this for loop is finished we will return the whole array back to wherever
we call this.
So let's go ahead and let's try it out.
Let's go ahead and call download service instance download.
Let's think what was it.
Download a trending repo's I believe it's going to actually return all the values we need.
This will be read.
Well let's just call it repots array and we'll print out repots array dashes zero and we're gonna take
the image and I'm going to print out the size because if we actually get a real image should print out
a size for us so let's go ahead and build and run.
Let's go test this we're doing a lot of kind of finicky data stuff but to make sure that we're casting
it properly we're going to just see how we do.
Let's let's go ahead and check it out.
OK.
So we're getting a problem here.
It's saying unexpectedly found nyl while unwrapping an optional value.
What we're going to do is we're going to head over to the next video and we're going to troubleshoot
this.
We're going to figure out why we're getting optional values why things aren't coming in the way we want
and we'll see in the next video.
So let's head over to the next one and let's fix this problem.