1
00:00:08,090 --> 00:00:11,020
Everybody what's going on this is Caleb with Deb's Lopes dot com.

2
00:00:11,040 --> 00:00:16,500
And in this video we're going to add a few more functions to our download service Singleton that is

3
00:00:16,500 --> 00:00:24,270
basically going to convert our array of dictionaries into instances of our repo model using those dictionaries

4
00:00:24,270 --> 00:00:25,300
which is pretty neat.

5
00:00:25,530 --> 00:00:30,760
And then we're going to set it up so that we can actually download that data pass it into some cells

6
00:00:30,760 --> 00:00:32,220
it's going to be super cool.

7
00:00:32,220 --> 00:00:34,490
Let's go ahead and let's start now.

8
00:00:34,620 --> 00:00:36,100
Pull open that project.

9
00:00:36,330 --> 00:00:37,670
And here we go.

10
00:00:37,860 --> 00:00:40,750
Let's go ahead and below trending repots data.

11
00:00:40,770 --> 00:00:42,940
Let's go ahead and actually stop that from running.

12
00:00:42,950 --> 00:00:47,760
Let's close the console and you know what I'm going to make some space here so you can actually see

13
00:00:47,760 --> 00:00:48,890
what I'm doing.

14
00:00:49,050 --> 00:00:54,600
And we're going to write a function that is called download trending repo's and we're going to be able

15
00:00:54,600 --> 00:01:00,450
to call this function here download trending repots directory and it's going to return Of course an

16
00:01:00,450 --> 00:01:01,800
array of dictionaries.

17
00:01:01,800 --> 00:01:06,330
Then what we're going to do is we're going to cycle through all those dictionaries and we're going to

18
00:01:06,330 --> 00:01:12,020
pull out the individual data and then return an instance of repository.

19
00:01:12,060 --> 00:01:12,300
OK.

20
00:01:12,300 --> 00:01:14,460
Or repo I guess is the short name.

21
00:01:14,460 --> 00:01:21,600
So go ahead and call phunk download WIPs download trending repo.

22
00:01:21,780 --> 00:01:22,470
All right.

23
00:01:22,800 --> 00:01:24,180
And just like before.

24
00:01:24,210 --> 00:01:26,640
We're going to use a completion handler that is escaping.

25
00:01:26,640 --> 00:01:31,680
So go ahead and type completion at escaping and if you missed my explanation in the last video go ahead

26
00:01:31,680 --> 00:01:33,200
and watch it again back there.

27
00:01:33,200 --> 00:01:39,340
Basically this handler is going to let us return values within a closure.

28
00:01:39,480 --> 00:01:43,330
It lets them escape from the closure instead of getting stuck.

29
00:01:43,380 --> 00:01:47,820
We're going to put that function here and that is the function that we pass those values into.

30
00:01:47,850 --> 00:01:54,340
It returns an empty function but the return is not what's important what is is the array of repo's.

31
00:01:54,570 --> 00:01:59,910
And as you remember we are not allowed to use named parameters in function types so we're going to go

32
00:01:59,910 --> 00:02:06,050
ahead and use an underscore and use the internal parameter of repots array.

33
00:02:06,330 --> 00:02:06,920
OK.

34
00:02:07,230 --> 00:02:11,000
And it's going to be an array of type repo like so.

35
00:02:11,250 --> 00:02:11,710
All right.

36
00:02:12,570 --> 00:02:14,000
So that's great.

37
00:02:14,310 --> 00:02:18,200
And that's what we're going to return as an array of type repo.

38
00:02:18,210 --> 00:02:20,590
So let's go ahead and let's call.

39
00:02:20,700 --> 00:02:22,860
Download trending repo's Diptera.

40
00:02:23,070 --> 00:02:27,710
That's our function from this class and go and press enter on that completion handler.

41
00:02:27,750 --> 00:02:30,000
This is the repo's dict array that we get.

42
00:02:30,000 --> 00:02:34,070
So we're going to call this trending repots dict.

43
00:02:34,560 --> 00:02:38,170
And now we have access to this property once it's done downloading.

44
00:02:38,310 --> 00:02:42,930
We can go ahead and start parsing through all of those dictionaries and doing something with each one

45
00:02:42,930 --> 00:02:43,670
of them.

46
00:02:43,680 --> 00:02:51,110
So let's go ahead and let's just say for dict in trending repo's dict array.

47
00:02:51,270 --> 00:02:54,040
Now we have access to each and every dictionary.

48
00:02:54,120 --> 00:02:59,600
What we're going to go ahead and do is basically set up each set up.

49
00:02:59,640 --> 00:03:08,790
Each property needed for instance of repo K and in order to do that I found that it's easier to use

50
00:03:08,790 --> 00:03:14,780
a function that we can pass in a dictionary parse through it all and then return the repo that we need.

51
00:03:14,790 --> 00:03:23,010
So let's go ahead and do that by calling phunk download trending repo.

52
00:03:23,040 --> 00:03:23,490
And you know what.

53
00:03:23,490 --> 00:03:28,620
This function should have an S for plural because it's going to return more than one download trending

54
00:03:28,620 --> 00:03:32,940
Ribault with with no s is only going to return one function.

55
00:03:32,970 --> 00:03:38,350
And so we're going to go ahead and use this function to return a repo like so.

56
00:03:39,000 --> 00:03:44,970
And you know of course we need to basically download a trending Ribault from a dictionary.

57
00:03:45,330 --> 00:03:45,770
OK.

58
00:03:45,870 --> 00:03:51,750
And that's going to be a dictionary of type string and any like so.

59
00:03:52,110 --> 00:03:53,010
But you know what.

60
00:03:53,010 --> 00:03:56,300
We need an internal parameter of dep't like so.

61
00:03:56,800 --> 00:04:02,430
And yeah so let's say that we pass in a dictionary like this one here we pass in a dictionary we're

62
00:04:02,430 --> 00:04:06,690
going to go ahead and basically create the properties that we need.

63
00:04:06,690 --> 00:04:10,120
So let's create an instance of repo so that we know what we need to fill in.

64
00:04:10,410 --> 00:04:14,110
Let Ribault equals repo wups repo.

65
00:04:14,370 --> 00:04:19,470
And we're going to need an image a name description number of fork's language number of contributors

66
00:04:19,500 --> 00:04:21,170
and repo you are l.

67
00:04:21,180 --> 00:04:27,240
So let's go ahead and let's think about what we need for the image.

68
00:04:27,240 --> 00:04:32,650
We're going to go ahead and just say let avatar you RL And you know what.

69
00:04:32,670 --> 00:04:38,880
If you remember in the GET have API we have an avatar you are l here and that's going to be the image

70
00:04:39,090 --> 00:04:41,900
for the user that created this repository.

71
00:04:42,150 --> 00:04:44,130
So we're going to use avatar you are ill.

72
00:04:44,640 --> 00:04:49,440
And if you look you'll see that the key name is actually Avatar underscore your L So I'm going to copy

73
00:04:49,440 --> 00:04:57,300
that and we're going to go ahead and say that that comes from dict and the key of course is named avatar

74
00:04:57,300 --> 00:04:58,510
you are l.

75
00:04:58,530 --> 00:05:00,980
Now that Avatar your all comes in as a string.

76
00:05:01,020 --> 00:05:07,430
And so since we're using a dictionary here we're going to go ahead and just call as string.

77
00:05:07,470 --> 00:05:11,580
We're going to force cast it as a string just so that it comes in as the exact type we need.

78
00:05:11,970 --> 00:05:14,910
And now that's going to be used later on.

79
00:05:14,910 --> 00:05:19,290
We can't yet download images because we have not written that function so we're not going to use it

80
00:05:19,290 --> 00:05:19,520
yet.

81
00:05:19,530 --> 00:05:20,810
But it is there.

82
00:05:20,820 --> 00:05:28,470
So next we need a name so let's just go ahead and type let name and we need one T-nut too.

83
00:05:28,740 --> 00:05:37,830
And that's going to come from dict K and the property in the actual dictionary is name just like you

84
00:05:37,830 --> 00:05:38,610
might expect.

85
00:05:38,610 --> 00:05:43,720
So go ahead and paste in name and cast it as a string.

86
00:05:43,770 --> 00:05:48,780
Next we need the description with one T.

87
00:05:50,040 --> 00:05:51,900
And that's going to come from dictionary.

88
00:05:52,050 --> 00:05:55,270
With square brackets and the description.

89
00:05:55,410 --> 00:05:58,870
If you go ahead and you look through we have the full name here.

90
00:05:59,250 --> 00:06:05,400
We have a description at the swift programming language so I'm going to go ahead and just paste in description

91
00:06:05,790 --> 00:06:07,590
and cast it as a string.

92
00:06:07,890 --> 00:06:09,230
Ok that's cool.

93
00:06:09,330 --> 00:06:13,260
And you know what since we're doing this we're going to go ahead and just let's give it the name let's

94
00:06:13,260 --> 00:06:15,980
give it the description.

95
00:06:16,020 --> 00:06:20,330
Now we need a number of forks.

96
00:06:21,900 --> 00:06:26,870
Number of forks is going to be equal to dict.

97
00:06:26,890 --> 00:06:31,090
We're going to put some quotes there and we're going to cast that as a string eventually.

98
00:06:31,240 --> 00:06:32,040
And you know what.

99
00:06:32,380 --> 00:06:34,170
All of these are going to come in as a string.

100
00:06:34,210 --> 00:06:35,730
So I'm going to copy that and use that later.

101
00:06:35,780 --> 00:06:45,670
But number of forks if you look you'll see that there is a property somewhere in here called forks right

102
00:06:45,670 --> 00:06:45,890
here.

103
00:06:45,910 --> 00:06:47,050
Fork's count.

104
00:06:47,110 --> 00:06:50,650
So this is the number of times that swift in particular has been forked.

105
00:06:50,650 --> 00:06:57,420
So go ahead and just remember that fork's underscore count K that's great.

106
00:06:57,430 --> 00:07:01,480
And we need it to be a string so we're casting it as a string but that's not actually going to work

107
00:07:01,990 --> 00:07:04,640
because the type that comes in is actually an INT.

108
00:07:04,840 --> 00:07:07,990
And so we would cause problems if we tried to cast that as a string.

109
00:07:07,990 --> 00:07:10,970
So what we're going to do is cast that as an integer.

110
00:07:10,990 --> 00:07:18,910
And now we're going to move on to let language equals dictionary and in quotes we're going to go ahead

111
00:07:18,940 --> 00:07:25,150
and look for language somewhere in here I believe it actually is up at the top.

112
00:07:25,400 --> 00:07:26,150
Maybe not

113
00:07:31,480 --> 00:07:31,730
OK.

114
00:07:31,740 --> 00:07:34,160
So language is here it says that it's C++.

115
00:07:34,170 --> 00:07:37,650
That's obviously not correct but for the most part it will be correct.

116
00:07:37,680 --> 00:07:40,820
So just call language and that's going to be a string.

117
00:07:41,220 --> 00:07:44,850
Then we need a number of contributors

118
00:07:47,290 --> 00:07:51,920
K and that whip's contributors and that's going to come from our dictionary.

119
00:07:52,220 --> 00:07:58,570
The contributor's do you see anything here for a number of contributors I don't.

120
00:07:58,590 --> 00:08:01,580
And that's because that's not publicly available on the front page.

121
00:08:01,590 --> 00:08:08,250
But there is a contributor's you r L and K now what we're going to do in the meanwhile is we're just

122
00:08:08,250 --> 00:08:12,810
going to go ahead and pass in a value for a number of contributors and then later on we're going to

123
00:08:12,810 --> 00:08:16,660
go ahead and write a function that can download the contributor's data.

124
00:08:16,770 --> 00:08:19,270
So let's go ahead and set number of forks.

125
00:08:19,800 --> 00:08:25,020
OK let's go ahead and set language and for a number of contributors let's just set one two three for

126
00:08:25,020 --> 00:08:28,920
now and then you know what we can set the repo you are I'll as well.

127
00:08:28,920 --> 00:08:35,880
So let's go ahead and type let repo you r l and that's going to come from the dictionary.

128
00:08:36,350 --> 00:08:37,050
And the dictionary.

129
00:08:37,050 --> 00:08:38,840
If you look for Ribault you are l.

130
00:08:38,850 --> 00:08:45,030
We got a property right here called HVM l u r l k and that is where we get the get hub link.

131
00:08:45,030 --> 00:08:51,540
So h t m l u r l That's Type that in h t m l u r l and of course that's going to get cast as a string

132
00:08:51,900 --> 00:08:52,870
and passed in here.

133
00:08:54,370 --> 00:08:57,060
Wups Ribault you are perfect.

134
00:08:57,130 --> 00:08:58,040
Now that's great.

135
00:08:58,060 --> 00:09:04,780
But for the image we're going to go ahead and type you I image and we're gonna call you I image named

136
00:09:04,870 --> 00:09:06,130
and we're going to pass in a name.

137
00:09:06,130 --> 00:09:09,210
Now if you remember I don't so that's why I'm going back to look.

138
00:09:09,220 --> 00:09:13,510
We're going to use search Eikon large just for now and then later when we actually populate it with

139
00:09:13,510 --> 00:09:21,430
a real image we will use that image of course but we're going to go ahead and unwrap that and get rid

140
00:09:21,430 --> 00:09:24,700
of this number of contributors for now until we actually need it.

141
00:09:25,060 --> 00:09:31,440
But then we're going to go ahead and just return the repo because this function is supposed to return

142
00:09:31,440 --> 00:09:32,640
a repo for us.

143
00:09:32,860 --> 00:09:36,430
So let's go back to our trending repots function here.

144
00:09:36,430 --> 00:09:41,020
And we just went through and we set up every property needed for a repository.

145
00:09:41,140 --> 00:09:45,670
It is a static repository for the image and for the contributors but everything else is coming in from

146
00:09:45,670 --> 00:09:47,620
the API which is cool.

147
00:09:47,620 --> 00:09:50,820
So in this function Here's what we're going to do.

148
00:09:51,130 --> 00:09:57,640
We're going to go ahead and delete this Babai and we're going to go ahead and call self download trending

149
00:09:57,640 --> 00:10:02,850
repo from dictionary and voila we have one dep't right here from our for loop.

150
00:10:02,860 --> 00:10:05,340
But remember this function returns a repo.

151
00:10:05,340 --> 00:10:08,080
So this itself is a repo value.

152
00:10:08,170 --> 00:10:15,880
So let's just go ahead and call this repo and set it to be equal to our return from our function and

153
00:10:15,940 --> 00:10:19,980
we need a way to kind of keep track of this because we're going to be returning an array of repo's.

154
00:10:20,020 --> 00:10:24,570
So let's create a property at the top that we can just start appending these repos to as they come in.

155
00:10:24,850 --> 00:10:31,340
Let's just say var repots array equals array of repo and we're going to instantiate it.

156
00:10:31,600 --> 00:10:38,300
And once we create one from our download trending repo we're just going to say repots array append.

157
00:10:38,320 --> 00:10:40,820
And we're going to pass it the repo just like that.

158
00:10:41,200 --> 00:10:49,420
But when we're finished we need to basically tell it that we're finished and return our repots array.

159
00:10:49,450 --> 00:10:54,310
So go ahead and just call completion and pass it the repots array.

160
00:10:54,310 --> 00:10:56,980
OK we've already set it to be 10.

161
00:10:57,070 --> 00:11:02,260
We're going to return 10 and upon completion we're going to basically just say all right these replies

162
00:11:02,260 --> 00:11:03,750
are good to go they're done.

163
00:11:03,790 --> 00:11:08,300
So let's go ahead let's go call this and let's see if we can get that data.

164
00:11:08,890 --> 00:11:09,830
Let's just see how we did.

165
00:11:09,850 --> 00:11:13,080
So let's call it download service.

166
00:11:13,120 --> 00:11:20,560
Instance whip's instance and we're going to just say download trending repots completion is an array

167
00:11:20,560 --> 00:11:25,410
of repo so let's just say return or sorry repo array.

168
00:11:25,600 --> 00:11:33,550
And we're going to go ahead and just print reape it well let's see it's an array of repo's so that we'll

169
00:11:33,550 --> 00:11:36,680
just print out a bunch of information.

170
00:11:36,820 --> 00:11:42,460
Why don't we print out the first Ribault and then let's print out.

171
00:11:42,730 --> 00:11:46,680
Well that should print out all the information so we'll just print out the first one that comes in.

172
00:11:46,690 --> 00:11:48,790
So go ahead and build and run.

173
00:11:48,910 --> 00:11:50,130
Let's go see how we did.

174
00:11:50,140 --> 00:11:51,310
Here we go.

175
00:11:51,580 --> 00:11:52,360
It's going to build.

176
00:11:52,360 --> 00:11:56,560
It's going to download all of our stuff and we'll see if we did all right.

177
00:11:56,590 --> 00:11:57,230
OK.

178
00:11:57,430 --> 00:12:01,660
Take a look it says fatal error index out of range.

179
00:12:01,660 --> 00:12:04,120
So that means that we're not getting anything back.

180
00:12:04,120 --> 00:12:08,740
Let's go ahead and let's build and run again let's make sure that we're just getting an empty array.

181
00:12:08,830 --> 00:12:10,480
That's probably the problem here.

182
00:12:12,770 --> 00:12:14,180
Here we go.

183
00:12:14,400 --> 00:12:15,100
OK.

184
00:12:15,230 --> 00:12:15,970
Check it out.

185
00:12:16,190 --> 00:12:21,980
We are getting an empty array and it does not like what we're doing so avatar you are l it is saying

186
00:12:21,980 --> 00:12:26,150
we are not getting a value for Avatar you are l it's coming in as nil.

187
00:12:26,480 --> 00:12:28,730
So you know what we're not actually using this.

188
00:12:28,790 --> 00:12:34,970
So let's go ahead and let's just blank blank that out like so and let's try running this one more time.

189
00:12:34,970 --> 00:12:35,670
Here we go.

190
00:12:40,960 --> 00:12:47,370
Let's see if that helps our problem k we get an empty array that's great.

191
00:12:47,390 --> 00:12:52,990
OK so it appears that not every repository has an avatar you or else so we're going to have to do something

192
00:12:52,990 --> 00:12:58,720
in a future video to basically only download repositories that do have an avatar you're L..

193
00:12:59,020 --> 00:13:05,980
But we are getting a bit of a problem here because we are not returning any values and that's because

194
00:13:05,980 --> 00:13:10,330
again we're completing this outside of our closure.

195
00:13:10,330 --> 00:13:14,690
So let's go ahead and put our completion at the bottom of our four loop here.

196
00:13:14,710 --> 00:13:15,770
Let's build and run.

197
00:13:15,940 --> 00:13:19,530
And that should successfully return our repots array.

198
00:13:19,540 --> 00:13:24,430
So let's go ahead and let's build it let's run it and let's see if we actually download data.

199
00:13:24,770 --> 00:13:25,310
OK.

200
00:13:25,570 --> 00:13:26,020
Look at that.

201
00:13:26,020 --> 00:13:31,130
So we're printing out the repo's array but it's just showing that these are instances of repo.

202
00:13:31,270 --> 00:13:36,370
So since we're getting all these repo's Let's count them one two three four five six seven eight nine

203
00:13:36,370 --> 00:13:37,120
10 11.

204
00:13:37,120 --> 00:13:37,870
Interesting.

205
00:13:38,080 --> 00:13:40,900
So we're actually getting 11 back o.

206
00:13:41,110 --> 00:13:41,380
Right.

207
00:13:41,380 --> 00:13:45,980
That's because of zero indexing so if we want 10 Let's go hadn't asked for nine.

208
00:13:46,350 --> 00:13:46,900
All right.

209
00:13:46,900 --> 00:13:53,920
So download service trending feed Visi let's print out the first item in the repo array and let's see

210
00:13:53,920 --> 00:13:58,300
all the values that we get when we do that building and running one more time.

211
00:13:58,300 --> 00:13:59,340
And here we go.

212
00:14:01,760 --> 00:14:03,170
OK of course sorry.

213
00:14:03,420 --> 00:14:05,640
Let's actually print out the values.

214
00:14:05,790 --> 00:14:07,350
How about the name.

215
00:14:07,410 --> 00:14:08,590
Print out the name.

216
00:14:09,190 --> 00:14:09,430
OK.

217
00:14:09,430 --> 00:14:11,180
That'll make a little more sense.

218
00:14:13,190 --> 00:14:13,970
All right it's building.

219
00:14:13,970 --> 00:14:15,360
Here we go.

220
00:14:15,860 --> 00:14:17,430
And we should get swift.

221
00:14:17,600 --> 00:14:22,270
There we go because remember in Safari here Swift is the first one that we get.

222
00:14:22,520 --> 00:14:28,670
So we're downloading 10 repositories and they happen to be coming in exactly as we want.

223
00:14:28,670 --> 00:14:34,070
We are so so close guys in the next video we're going to actually write a function that will download

224
00:14:34,070 --> 00:14:35,410
the avatar image.

225
00:14:35,630 --> 00:14:42,740
We're going to download the contributor's data and we're also going to make sure that all of the repositories

226
00:14:42,740 --> 00:14:45,720
that are coming in have all the properties that we need.

227
00:14:45,770 --> 00:14:46,950
Otherwise we'll just kick them out.

228
00:14:46,970 --> 00:14:49,240
So we're going to go ahead and delete this.

229
00:14:49,280 --> 00:14:53,270
We're going to go ahead and head back to download service and in the next video we're going to make

230
00:14:53,270 --> 00:14:53,770
it happen.

231
00:14:53,810 --> 00:14:54,560
Let's go do that.

232
00:14:54,560 --> 00:14:57,110
Let's head over to the next video and download all that good stuff.
