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