1
00:00:08,010 --> 00:00:09,060
Hey buddy what's going on.

2
00:00:09,060 --> 00:00:14,700
This is Caleb with Debb slopes dot com and in this video we're going to talk about optionals and optionals

3
00:00:14,700 --> 00:00:17,970
are an amazing part of the swift programming language.

4
00:00:17,970 --> 00:00:22,560
And I want you to think about an optional sort of like a rapper or like a box.

5
00:00:22,650 --> 00:00:23,280
OK.

6
00:00:23,310 --> 00:00:26,970
Now it can hold something right like a box can have something inside of it.

7
00:00:26,970 --> 00:00:32,790
A wrapper can have some type of maybe food or even like a drink if you think of like a drink container

8
00:00:33,840 --> 00:00:36,710
it can hold something or it can be empty.

9
00:00:36,750 --> 00:00:37,020
OK.

10
00:00:37,050 --> 00:00:42,390
That's like that's what an optional is sort of like it can have a value or it cannot have a value.

11
00:00:42,390 --> 00:00:45,850
Now imagine you got a package delivered from Amazon.

12
00:00:45,960 --> 00:00:52,200
When you pick it up off your front porch there can be something inside but it's possible that at the

13
00:00:52,200 --> 00:00:58,260
fulfillment center there was a mishap and maybe your box is empty which of course that would be really

14
00:00:58,260 --> 00:01:02,010
disappointing and kind of a lame thing but that is possible.

15
00:01:02,010 --> 00:01:08,910
The idea behind optionals is that we can safely use the box without having to worry what's inside.

16
00:01:09,130 --> 00:01:09,800
OK.

17
00:01:10,050 --> 00:01:17,300
That is the cool thing about optionals So let's go ahead let's pull open a brand new X code playground.

18
00:01:17,370 --> 00:01:23,580
So open up X code and in this little welcome to X code menu click get started with a playground and

19
00:01:23,610 --> 00:01:31,480
we're going to do a blank playground and I'm going to call this shoes optional.

20
00:01:31,600 --> 00:01:37,290
Just you know instead of clothing optional just say shoes optional little more appropriate anyway create

21
00:01:37,290 --> 00:01:39,510
it and let's pull it open.

22
00:01:39,570 --> 00:01:40,800
Full size.

23
00:01:40,800 --> 00:01:41,570
There we go.

24
00:01:41,610 --> 00:01:47,130
By the way if you're wondering how I'm doing that I can make it take up the left side of the screen

25
00:01:47,160 --> 00:01:48,750
the right side of the screen.

26
00:01:48,780 --> 00:01:49,670
Anything like that.

27
00:01:49,680 --> 00:01:50,230
Pretty neat.

28
00:01:50,250 --> 00:01:53,550
It's a little app called shoot.

29
00:01:53,550 --> 00:01:55,620
What is it called a magnet.

30
00:01:55,620 --> 00:01:56,140
Right.

31
00:01:56,190 --> 00:01:56,810
It's called Magnet.

32
00:01:56,810 --> 00:01:58,380
There's another one called flexi glass.

33
00:01:58,380 --> 00:02:00,990
I just happen to like magnet a little bit better.

34
00:02:01,020 --> 00:02:02,340
Anyway let's get started.

35
00:02:02,370 --> 00:02:08,220
So I'm going to go ahead and get rid of that variable there and I'm going to get rid of that little

36
00:02:08,220 --> 00:02:10,420
commented line there.

37
00:02:10,560 --> 00:02:15,830
And I want to show you how optionals can work and I'm going to use some images to represent them.

38
00:02:15,960 --> 00:02:20,640
So I'm going to create an optional image and I'm going to create a non optional image so I'm going to

39
00:02:20,640 --> 00:02:23,250
show you how you declare a variable as optional.

40
00:02:23,550 --> 00:02:32,370
So go ahead and type let optional image and we're going to make it of type UI image but I'm going to

41
00:02:32,370 --> 00:02:37,830
declare it is optional by putting a question mark at the end of the type declaration.

42
00:02:37,830 --> 00:02:41,280
Now of course as it currently exists it is optional.

43
00:02:41,280 --> 00:02:42,830
It can have a value.

44
00:02:42,870 --> 00:02:50,220
It can not have a value meaning it's able to be nil or it's able to be or to have a value.

45
00:02:50,370 --> 00:02:55,800
And I'm going to go ahead and give it a value just for the explanation purposes of this.

46
00:02:56,030 --> 00:03:01,980
And so I'm going to insert an image literal from the editor menu and I'm going to drop in that image

47
00:03:01,980 --> 00:03:02,740
right there.

48
00:03:02,970 --> 00:03:06,820
OK now I'm going to add a viewer so we can see the image.

49
00:03:06,930 --> 00:03:09,360
All righty and that's great.

50
00:03:09,360 --> 00:03:10,400
So there it is.

51
00:03:10,410 --> 00:03:16,260
This is an image representation of what an optional is like it's a box but it can be empty meaning it

52
00:03:16,260 --> 00:03:18,090
can have a nil value.

53
00:03:18,090 --> 00:03:25,680
Now let's go ahead and create let non optional image and we're going to make that of Type II image K

54
00:03:25,710 --> 00:03:29,430
and we're not going to put a question mark meaning it has a literal value.

55
00:03:29,430 --> 00:03:35,060
We're going to go ahead and insert an image literal and I'm going to use this image to represent that.

56
00:03:35,100 --> 00:03:38,130
And when I pull open this viewer you can see what I mean.

57
00:03:38,130 --> 00:03:40,330
This non-optional image.

58
00:03:40,590 --> 00:03:45,150
Well it's an image but it represents an optional with a value of five.

59
00:03:45,150 --> 00:03:49,790
So this optional can have a value of 5 or can have a value of nil.

60
00:03:49,980 --> 00:03:51,450
That's pretty cool.

61
00:03:51,450 --> 00:03:56,250
Now I'm going to show you ways that you can actually use optionals and then some things we need to be

62
00:03:56,250 --> 00:04:01,390
careful about as well as at the end I'm going to show you kind of a real life example of using optionals.

63
00:04:01,590 --> 00:04:05,710
So we're going to go ahead and talk about forced unwrapping.

64
00:04:05,760 --> 00:04:07,980
So go ahead and type a variable here.

65
00:04:07,980 --> 00:04:12,080
Var optional number k.

66
00:04:12,420 --> 00:04:14,610
And we're going to make it of type int.

67
00:04:14,670 --> 00:04:16,560
Although we're going to make it optional.

68
00:04:16,770 --> 00:04:22,060
OK so this could be for something maybe that we don't know if it's going to have a value or not.

69
00:04:22,320 --> 00:04:29,130
Although I'm going to set it to have a value of 5 K so it's optional but it has a value of 5 and I'll

70
00:04:29,130 --> 00:04:31,980
show you how this works in just a moment.

71
00:04:31,980 --> 00:04:38,760
Next we're going to go ahead and just create another variable of type and and make it equal to 5 as

72
00:04:38,790 --> 00:04:39,210
well.

73
00:04:39,210 --> 00:04:42,020
Now you'll notice the second one is not optional.

74
00:04:42,150 --> 00:04:53,120
So what I can do here is I can say if optional number is not equal to nil OK meaning if it is not nil.

75
00:04:53,310 --> 00:05:01,290
We're going to print and we're going to put something in the print statement that says optional number

76
00:05:02,460 --> 00:05:11,660
has a value of and will use string interpolation here to pull out the value of optional number and we

77
00:05:11,660 --> 00:05:13,510
will go like that and pass it.

78
00:05:13,510 --> 00:05:17,810
The value for optional number because we have given it a value right.

79
00:05:18,140 --> 00:05:22,610
We say optional number has a value of optional number.

80
00:05:22,610 --> 00:05:28,190
Now you'll notice when it prints out it says optional number has a value of optional 5.

81
00:05:28,190 --> 00:05:31,190
Now you're wondering well why is that.

82
00:05:31,220 --> 00:05:34,840
This is a way that swift keeps a safe when we're programming.

83
00:05:34,970 --> 00:05:40,760
Since it is optional it's passing in the value for 5 but it's keeping track that it's an optional value

84
00:05:40,760 --> 00:05:42,440
and that it could be nil.

85
00:05:42,440 --> 00:05:50,130
Now watch what happens if I change it to nil meaning I give it a nil value of value of nothing.

86
00:05:50,150 --> 00:05:55,180
You'll notice nothing happens because we're checking to see if optional number is nil.

87
00:05:55,220 --> 00:05:57,400
So let's turn it back to 5.

88
00:05:57,560 --> 00:06:00,950
But I said we're talking about force unwrapping.

89
00:06:00,950 --> 00:06:05,260
Now this optional number variable has an optional value of 5.

90
00:06:05,270 --> 00:06:11,720
But in order to actually access the real value we need to use an exclamation mark to force unwrap it.

91
00:06:11,840 --> 00:06:14,780
It's like we're opening the box and taking the value 5 out.

92
00:06:14,780 --> 00:06:17,210
We're actually getting it from the box.

93
00:06:17,210 --> 00:06:18,720
So that's Force unwrapping.

94
00:06:18,740 --> 00:06:22,530
But watch what happens if I change this.

95
00:06:22,550 --> 00:06:26,350
Let's just say I take away this conditional check.

96
00:06:26,390 --> 00:06:29,370
This is to show you the danger of using an optional.

97
00:06:29,510 --> 00:06:38,310
Watch what happens if I change it to nil fatal error unexpectedly found nil while unwrapping an optional

98
00:06:38,310 --> 00:06:39,270
value.

99
00:06:39,660 --> 00:06:44,200
That's a problem so this is where we need to be careful with force on wrapping.

100
00:06:44,400 --> 00:06:49,830
You need to basically set up conditional code to check to make sure that it's not nil.

101
00:06:50,190 --> 00:06:53,280
And you might be wondering well why would I ever have an optional value.

102
00:06:53,280 --> 00:06:57,300
Think about if you're pulling data from a server and you're not sure that a certain value will come

103
00:06:57,300 --> 00:06:59,590
back of a certain type every single time.

104
00:06:59,610 --> 00:07:06,450
It would be better to set it as nil or sorry I said as an optional in case the value returns nil so

105
00:07:06,570 --> 00:07:08,930
if that were to happen you could say if it's not nil.

106
00:07:08,940 --> 00:07:10,870
Do something else.

107
00:07:11,130 --> 00:07:15,410
And then here you could basically set it up so that it would handle any errors.

108
00:07:15,440 --> 00:07:18,670
Ok that is just one way you could use optionals.

109
00:07:18,680 --> 00:07:20,370
Kate gives us the option right.

110
00:07:20,370 --> 00:07:21,620
That's the optional thing.

111
00:07:21,750 --> 00:07:27,210
It gives us the option of basically using a value or a non-value.

112
00:07:27,210 --> 00:07:27,900
So here we go.

113
00:07:27,900 --> 00:07:28,920
Let's go ahead and move on.

114
00:07:28,920 --> 00:07:30,050
That's for Sun wrapping.

115
00:07:30,060 --> 00:07:37,110
That's how you take an optional value and you force unwrap it into something that is an actual value.

116
00:07:37,110 --> 00:07:37,950
Pretty cool.

117
00:07:38,130 --> 00:07:41,770
Now I'm going to go ahead and just close these viewers here they're giving us some trouble.

118
00:07:42,150 --> 00:07:43,670
And let's move on.

119
00:07:43,890 --> 00:07:49,070
So next up we're going to go ahead and check out something called optional binding.

120
00:07:49,250 --> 00:07:49,900
OK.

121
00:07:50,070 --> 00:07:54,630
Now what we're going to do with optional binding is we're going to basically take an optional value

122
00:07:54,840 --> 00:07:58,050
and we're going to bind it to a non optional constant.

123
00:07:58,050 --> 00:08:04,830
But the cool thing is that we will use an IF statement to handle what happens if the optional number

124
00:08:04,830 --> 00:08:05,730
comes in as nil.

125
00:08:05,730 --> 00:08:06,800
So check it out.

126
00:08:06,930 --> 00:08:14,430
If we go ahead and type if let And then we can say constant number K-Stew we just created a constant

127
00:08:14,430 --> 00:08:16,380
called constant number.

128
00:08:16,380 --> 00:08:20,240
We're going to set it to be equal to optional number.

129
00:08:20,250 --> 00:08:25,570
All right so now we have a constant that has a definite value of optional number.

130
00:08:25,860 --> 00:08:29,910
But what happens if constant number is not.

131
00:08:30,030 --> 00:08:34,330
What if we can't set the value of constant number what if optional number is actually nil.

132
00:08:34,380 --> 00:08:35,630
That would cause a problem.

133
00:08:35,820 --> 00:08:41,510
So what we're going to do is we're going to print let's print out what we see here.

134
00:08:41,700 --> 00:08:46,690
Constant number has a value of.

135
00:08:46,820 --> 00:08:49,890
And we'll print out constant number.

136
00:08:50,010 --> 00:08:50,640
OK.

137
00:08:50,970 --> 00:08:56,280
Now when this runs you'll see that it actually does print.

138
00:08:57,060 --> 00:09:04,070
We are creating a constant called constant number and when it runs lets run it and you'll see it'll

139
00:09:04,080 --> 00:09:08,440
print out constant number has a value of constant number but it's not doing anything.

140
00:09:08,640 --> 00:09:10,940
That's because our value is nil.

141
00:09:10,950 --> 00:09:15,840
And that's where our else block comes in when we're doing optional binding we're basically binding an

142
00:09:15,840 --> 00:09:17,730
optional value to a constant.

143
00:09:17,730 --> 00:09:22,110
But we're using an if else so that we can handle both cases value or no value.

144
00:09:22,110 --> 00:09:27,440
So if we can create the constant and set it to be optional number it'll print that out.

145
00:09:27,540 --> 00:09:32,520
Otherwise we will print what is it.

146
00:09:32,670 --> 00:09:38,610
Let's see optional number is nil because that's what it means it must be nil if that's if that's what

147
00:09:38,610 --> 00:09:41,880
is happening and as you can see optional number is nil.

148
00:09:41,880 --> 00:09:45,300
And so that is where we could handle errors just like I said above.

149
00:09:45,450 --> 00:09:52,230
But the cool thing is that we can use constant number as a way of creating a non optional value do you

150
00:09:52,230 --> 00:09:54,480
notice how we don't have to force on wrap this.

151
00:09:54,480 --> 00:10:00,780
Watch what happens when I set it to be a value of five optional number and Konst that number has a value

152
00:10:00,780 --> 00:10:06,700
of five that basically allows us to create a non-optional constant from an optional.

153
00:10:06,720 --> 00:10:07,910
Pretty cool.

154
00:10:07,950 --> 00:10:13,590
So that is optional binding you know what let me go ahead and put some notes here so we don't forget

155
00:10:13,680 --> 00:10:20,090
this was force unwrapping force unwrapping.

156
00:10:20,110 --> 00:10:21,640
Whoops.

157
00:10:22,210 --> 00:10:23,130
Anyway moving on.

158
00:10:23,380 --> 00:10:25,800
So let's go ahead and I want to show you another example.

159
00:10:25,810 --> 00:10:31,840
I'm going to create a function here called into printer and it's basically just going to print out an

160
00:10:31,840 --> 00:10:33,140
integer value.

161
00:10:33,130 --> 00:10:37,820
Now I can also use optional binding within the guard let check it out.

162
00:10:38,080 --> 00:10:42,050
Guard let constant number just like above.

163
00:10:42,340 --> 00:10:45,930
And we're going to set it to be equal to optional number.

164
00:10:46,210 --> 00:10:48,880
If we can't do that right.

165
00:10:48,880 --> 00:10:53,600
Meaning if it's nil we're just going to go ahead and say return just like that.

166
00:10:53,620 --> 00:11:02,260
Now assuming that we do get a value we're going to go ahead and print constant number has a value value

167
00:11:02,350 --> 00:11:02,920
of

168
00:11:06,010 --> 00:11:08,350
what is it optional number a constant number.

169
00:11:08,470 --> 00:11:11,640
We'll just say constant number because it will be the same as optional number.

170
00:11:11,650 --> 00:11:12,190
Cool cool.

171
00:11:12,190 --> 00:11:17,210
So we've used the guard let to say if optional number has a value.

172
00:11:17,230 --> 00:11:21,370
Set it to be equal to or set constant number to be equal to optional number.

173
00:11:21,400 --> 00:11:23,530
Otherwise return and get out of the function.

174
00:11:23,560 --> 00:11:25,890
So let's go ahead and let's call in printer.

175
00:11:26,020 --> 00:11:28,390
Whoops there we go.

176
00:11:28,480 --> 00:11:31,940
And when it runs you'll see that it prints it out right there a third time.

177
00:11:32,110 --> 00:11:37,260
But watch what happens if I set optional number to be equal to nil or have no value.

178
00:11:37,390 --> 00:11:38,500
It doesn't print.

179
00:11:38,500 --> 00:11:45,280
The only one that prints is our first one where we forced unwrap the value or sorry where it where we

180
00:11:45,610 --> 00:11:46,430
tried to bind it.

181
00:11:46,450 --> 00:11:50,030
But if we couldn't bind it then we would print out optional numbers nil.

182
00:11:50,230 --> 00:11:53,860
So change it back to have a value 5 and we're going to move on now.

183
00:11:54,010 --> 00:12:01,860
We're going to talk about what are called implicitly implicitly unwrapped optionals.

184
00:12:02,090 --> 00:12:02,700
OK.

185
00:12:03,000 --> 00:12:08,870
So implicitly unwrapped optional is basically where we are going to force unwrap a value from the very

186
00:12:08,870 --> 00:12:09,590
beginning.

187
00:12:09,710 --> 00:12:16,580
So to do that we can just say I'm going to assume that a value is coming in as maybe 5 so let assumed

188
00:12:17,240 --> 00:12:20,580
value of type int equals five.

189
00:12:20,600 --> 00:12:25,820
But if we're going to make this implicitly unwrapped we're going to go ahead and put an exclamation

190
00:12:25,820 --> 00:12:26,090
mark.

191
00:12:26,090 --> 00:12:28,860
That means we are force on wrapping that value.

192
00:12:28,880 --> 00:12:40,430
So what we're going to do next is we can just say let implicit value of type and equals assumed value.

193
00:12:40,830 --> 00:12:41,580
OK.

194
00:12:41,930 --> 00:12:46,850
So we are able to create a value you can see over here that it does create a value of 5 but watch what

195
00:12:46,850 --> 00:12:47,760
happens if I make it.

196
00:12:47,760 --> 00:12:53,730
Nil we get an error unexpectedly found nil while unwrapping an optional value.

197
00:12:53,970 --> 00:13:02,010
So unless you are one million percent sure do not implicitly unwrap optionals this is kind of a last

198
00:13:02,010 --> 00:13:02,640
ditch thing.

199
00:13:02,640 --> 00:13:06,480
It's kind of if you have to if you're not 100 percent sure that there's a value.

200
00:13:06,480 --> 00:13:09,860
Do not force on rap or implicitly unwrap values like that.

201
00:13:09,980 --> 00:13:11,980
OK it's not good practice.

202
00:13:12,420 --> 00:13:17,460
OK so next up there's kind of a really cool thing I want to show you with optionals called nyl coalescing

203
00:13:17,970 --> 00:13:21,030
and something called using the ternary operator.

204
00:13:21,030 --> 00:13:30,940
So let's go ahead and call and nyl coalescing and using turn very operator.

205
00:13:30,960 --> 00:13:33,110
So we're going to create an optional integer.

206
00:13:33,250 --> 00:13:42,780
OK so go ahead and type let optional end of type int equals nil and you'll see if we do this it's going

207
00:13:42,780 --> 00:13:49,700
to not let us it'll say we can't use nil on something of type int because nil is not an integer value.

208
00:13:49,920 --> 00:13:55,770
But if we set it to be an optional that means that we basically put that in a box and we say OK the

209
00:13:55,770 --> 00:14:02,790
box can have a nil value or it can have a numerical value so that's cool and all but what if we want

210
00:14:02,790 --> 00:14:07,830
to create a variable but we're not sure if optional end is going to have a value or not.

211
00:14:07,830 --> 00:14:14,750
Maybe we're pulling data from a server so we can say let results equals and watch what we can do.

212
00:14:14,880 --> 00:14:20,080
We can say it equals the optional and but what if you're not sure.

213
00:14:20,370 --> 00:14:25,350
Now if it comes back with value like if we gave it the value five that's great and now optional is equal

214
00:14:25,350 --> 00:14:28,390
to five therefore result is equal to five.

215
00:14:28,470 --> 00:14:33,210
But if it comes back nil we might not want to set it to be nil right.

216
00:14:33,210 --> 00:14:35,210
That would cause a crash in our application.

217
00:14:35,370 --> 00:14:40,410
So what we can do is we can use the turn every operator by doing two question marks.

218
00:14:40,410 --> 00:14:42,530
Then after that we can type is zero.

219
00:14:42,660 --> 00:14:49,200
And what this is basically saying and you'll see it as it prints out is if we don't get a value set

220
00:14:49,200 --> 00:14:50,020
it to zero.

221
00:14:50,190 --> 00:14:51,970
Otherwise set it to be optional.

222
00:14:51,970 --> 00:14:54,960
And so this is kind of like like else.

223
00:14:54,960 --> 00:14:55,230
Think.

224
00:14:55,260 --> 00:14:58,120
Think of it like this optional it has a value.

225
00:14:58,200 --> 00:15:00,110
Let results equal optional.

226
00:15:00,120 --> 00:15:03,610
And if there's a value otherwise set it to be zero.

227
00:15:03,660 --> 00:15:06,340
Meaning if Neal comes through it's going to set it to zero.

228
00:15:06,350 --> 00:15:12,090
Just like you can see now if I said it to five you'll see that the optional will print out as 5 because

229
00:15:12,090 --> 00:15:12,960
it adds value.

230
00:15:12,990 --> 00:15:14,770
Really really cool.

231
00:15:14,790 --> 00:15:19,480
So let's go and set it to nil just because it's pretty cool that it's using the ternary operator there

232
00:15:20,070 --> 00:15:26,070
and last but not least we're going to go ahead and use a real world example with Comic-Con.

233
00:15:26,070 --> 00:15:28,770
I'm going to Comic-Con next month and I'm really excited.

234
00:15:28,770 --> 00:15:33,000
So I was thinking about it and thought it was a perfect example for what we're going to do.

235
00:15:33,000 --> 00:15:38,700
So first of all we need to create a class for Comic-Con attendee and we need to create a class for their

236
00:15:38,700 --> 00:15:39,530
admission badge.

237
00:15:39,540 --> 00:15:50,400
So go ahead and type class comic con and the K and we need a variable we need an admission badge like

238
00:15:50,400 --> 00:15:57,690
so of type admission badge which That's a class will create in a second but we're going to make it optional

239
00:15:57,720 --> 00:16:02,700
because everybody who goes to Comic-Con they might not have a badge from the very beginning they might

240
00:16:02,700 --> 00:16:04,570
want to buy tickets when they get there.

241
00:16:04,860 --> 00:16:11,760
So we need to create a quick initialiser and we're just going to say badge of type admission badge and

242
00:16:11,790 --> 00:16:17,700
that of course is optional because they may or may not have a badge and we'll just say self admission

243
00:16:17,700 --> 00:16:22,260
badge is equal to badge just like that.

244
00:16:22,270 --> 00:16:27,290
OK so now we're going to create the admission badge class so that our attendees can actually attend.

245
00:16:27,300 --> 00:16:28,330
That would be helpful.

246
00:16:28,520 --> 00:16:37,310
So type class admission badge and let's go ahead and just say for Comic-Con we're just going to give

247
00:16:37,430 --> 00:16:39,680
a variable number of days.

248
00:16:39,680 --> 00:16:41,640
That's usually how Comic-Con tickets go.

249
00:16:41,930 --> 00:16:47,660
And every bag is going to have an explicit value so we don't need to make it optional and we just need

250
00:16:47,660 --> 00:16:48,640
to initialize it.

251
00:16:48,650 --> 00:16:58,100
So go ahead and type number of days of type int and we'll set self number of days to be equal to number

252
00:16:58,100 --> 00:16:59,180
of days.

253
00:16:59,180 --> 00:17:00,430
Very cool.

254
00:17:00,440 --> 00:17:04,160
So what we're going to do is we're going to create an admission badge.

255
00:17:04,220 --> 00:17:09,410
We're going to create a Comic-Con attendee and we're going to give them that admission badge so go ahead

256
00:17:09,410 --> 00:17:15,730
and type let admission badge equals admission badge and we're going to pass in a number of days.

257
00:17:15,740 --> 00:17:20,600
Let's just give them a three day pass and now we're going to create a Comic-Con attendee.

258
00:17:20,600 --> 00:17:27,890
So let attendee equals Comic-Con attendee and need to give them a badge so go ahead and pass in the

259
00:17:27,890 --> 00:17:29,570
admission badge.

260
00:17:29,570 --> 00:17:35,290
And just like that we have an admission badge and we have a Comic-Con attendee which is really cool.

261
00:17:35,480 --> 00:17:40,550
But if I was building an app where I wanted to show how many days left you have on your Comic-Con badge

262
00:17:40,880 --> 00:17:50,030
I might type something like this if let days a tenable meaning I have X amount of days left.

263
00:17:50,150 --> 00:17:58,040
We can check that by going in to attend the admission badge end did you notice admission badge is optional.

264
00:17:58,060 --> 00:18:02,820
You can see that right there and watch what happens when I type that number of days.

265
00:18:03,130 --> 00:18:06,100
That's because I might not have an admission badge.

266
00:18:06,150 --> 00:18:06,710
OK.

267
00:18:06,730 --> 00:18:11,800
So it's putting an optional there so that it is safely unwrapping and safely using the value in my admission

268
00:18:11,800 --> 00:18:13,190
badge.

269
00:18:13,510 --> 00:18:19,120
So if there's amendable is equal to number of days which in this case is three we're going to go ahead

270
00:18:19,150 --> 00:18:22,410
and print whips.

271
00:18:22,540 --> 00:18:31,190
We're going to print this attendee can enter Comic-Con for.

272
00:18:31,480 --> 00:18:37,410
We're going to print the number of days here days a tenable days.

273
00:18:37,680 --> 00:18:41,800
Else we're going to print something else.

274
00:18:41,800 --> 00:18:49,030
Now we didn't think what would we print if days a tenable is not equal or if days a tenable number of

275
00:18:49,030 --> 00:18:50,820
days comes back as.

276
00:18:51,010 --> 00:18:57,250
We need to say this person has not yet purchased a ticket.

277
00:18:57,370 --> 00:19:04,230
Please refer them to the ticketing window and watch what happens when we print this out.

278
00:19:04,300 --> 00:19:04,570
OK.

279
00:19:04,600 --> 00:19:12,070
So it says this attendee can enter Comic-Con for three days but watch what happens if I remove the admission

280
00:19:12,070 --> 00:19:17,730
badge remember for a Comic-Con attendee admission badge is optional so I can pass in Knill.

281
00:19:17,920 --> 00:19:21,030
What if I did not have an admission badge.

282
00:19:21,250 --> 00:19:22,630
If it comes in is nil.

283
00:19:22,630 --> 00:19:24,010
Watch what happens.

284
00:19:24,010 --> 00:19:26,020
This person has not yet purchased a ticket.

285
00:19:26,020 --> 00:19:33,670
Please refer them to the ticketing window because I was using optional binding and that basically created

286
00:19:33,670 --> 00:19:39,310
a constant here called days attend table and I set it to be equal to the number of days from the admission

287
00:19:39,310 --> 00:19:39,970
badge.

288
00:19:40,000 --> 00:19:45,920
But if the admission badge is nil then I get an optional value of nil and it prints out here.

289
00:19:45,940 --> 00:19:48,270
This person has not yet purchased the ticket.

290
00:19:48,280 --> 00:19:51,760
This is called Let me put it up here at the very top.

291
00:19:51,790 --> 00:19:58,750
This is called optional chaining and it allows us to change values of optionals together so that we

292
00:19:58,750 --> 00:20:05,710
can get a value or handle errors when it returns nil optionals are incredibly helpful and I really hope

293
00:20:05,710 --> 00:20:11,860
that you can see that these are just some super helpful small examples of using optionals And of course

294
00:20:11,890 --> 00:20:13,990
if you're confused or if you want to know more.

295
00:20:14,080 --> 00:20:16,720
I would highly recommend digging into the Apple documentation.

296
00:20:16,720 --> 00:20:18,680
There are tons of examples there.

297
00:20:18,820 --> 00:20:20,650
So amazing work with this video.

298
00:20:20,650 --> 00:20:22,350
And let's head over to the next one.
