1
00:00:02,400 --> 00:00:08,850
Hello and welcome back in this lesson you're going to talk about swift optionals swift optionals might

2
00:00:08,850 --> 00:00:11,510
seem a little bit confusing at the beginning.

3
00:00:11,640 --> 00:00:15,560
So I'm going to try to simplify it as much as I can in this lesson.

4
00:00:15,600 --> 00:00:22,500
So if the optionals has only one meaning to make sure that your valuables have a value when you use

5
00:00:22,500 --> 00:00:23,070
them.

6
00:00:23,070 --> 00:00:29,340
So essentially it's a way for us to make sure that you don't act as a value that doesn't have anything

7
00:00:29,340 --> 00:00:29,570
in it.

8
00:00:29,580 --> 00:00:34,470
You don't access any value which is going to cause your application to crash.

9
00:00:34,650 --> 00:00:39,030
So let's go ahead into export and see how we could actually use them.

10
00:00:39,360 --> 00:00:44,160
I'm going to go to school and I'm going to start a new Actually this time I'm going to start a new command

11
00:00:44,160 --> 00:00:45,250
line tool.

12
00:00:45,420 --> 00:00:52,950
I'm going to say command line tool strafed and then when I call it my option is to

13
00:00:55,580 --> 00:01:02,230
and my optional tours I'm going to produce three different variables.

14
00:01:02,260 --> 00:01:04,500
I'm not talking about all three of them.

15
00:01:04,610 --> 00:01:09,770
One of them I'm going to call it the optional string.

16
00:01:10,180 --> 00:01:13,380
And that's going to be a strong question mark.

17
00:01:13,790 --> 00:01:19,960
The other one I'm going to call it the VAR non optional string.

18
00:01:20,270 --> 00:01:26,240
It is of the type of string that you keep the same formatting and it says hello.

19
00:01:26,930 --> 00:01:30,460
And then the last one is going to be there.

20
00:01:31,520 --> 00:01:34,490
On the wrap string.

21
00:01:35,560 --> 00:01:37,840
And that's the type of string.

22
00:01:38,600 --> 00:01:41,970
So I want to spend a few seconds having a look at this.

23
00:01:42,020 --> 00:01:44,700
And then when I explain what they mean.

24
00:01:44,720 --> 00:01:51,650
So now let's try to understand what do these three things mean an optional a string with a string questionmark

25
00:01:51,740 --> 00:01:56,000
means this value could be nil.

26
00:01:56,180 --> 00:01:59,550
It's possible that it doesn't have anything inside it.

27
00:01:59,720 --> 00:02:06,080
So whenever you want to use it and we know the value exists we have to actually access it by saying

28
00:02:06,080 --> 00:02:11,230
something for instance like optional a string and we have to force it.

29
00:02:11,240 --> 00:02:14,730
We have to say now we know the value exists.

30
00:02:14,750 --> 00:02:20,820
Obviously this is not going to print nilla for us because it is not there but for us it means that it's

31
00:02:20,830 --> 00:02:24,060
likely you could be in in a few minutes.

32
00:02:24,060 --> 00:02:29,390
I will show you how we go about doing this in a safe way because this is really not a very safe way

33
00:02:29,390 --> 00:02:38,090
of forcing it because for instance if you set this value was my user name and it is nil because user

34
00:02:38,090 --> 00:02:41,370
may not have entered the username in the text field.

35
00:02:41,480 --> 00:02:49,520
And then if you had a function hypothetical function that says like innovate the user name and you entered

36
00:02:49,520 --> 00:02:50,610
this in here.

37
00:02:50,750 --> 00:02:55,130
Then obviously this is going to crash because it is going to say well you're trying to log in with something

38
00:02:55,130 --> 00:03:00,180
that doesn't exist all you're trying to do something like luggin with Ned.

39
00:03:00,350 --> 00:03:04,110
So I'll show you in a minute how we go about being safe with it.

40
00:03:04,130 --> 00:03:12,540
However whenever you have an optional string it means this could be nil.

41
00:03:12,970 --> 00:03:15,200
The other one is a non optional listing.

42
00:03:15,200 --> 00:03:21,530
The Raiders are getting more used to using it may not actually string a string and that has a value

43
00:03:21,530 --> 00:03:22,610
such as Hello.

44
00:03:22,700 --> 00:03:28,740
So I could actually take this out and go ahead in here and say Prince.

45
00:03:28,950 --> 00:03:31,210
Prince none optional string.

46
00:03:31,400 --> 00:03:36,610
So what do you think is going to happen when I run this application now before I can even run it.

47
00:03:36,610 --> 00:03:43,200
It tells me hey this variable needs to be initialized because it is not an option.

48
00:03:43,310 --> 00:03:45,010
It must have a value.

49
00:03:45,140 --> 00:03:49,440
So you either have to do it at a declaration time or you have to do it.

50
00:03:49,840 --> 00:03:52,190
Now this string is headed.

51
00:03:52,520 --> 00:03:56,000
So we'll use either of these two situations.

52
00:03:56,020 --> 00:04:01,080
We use optional values for when we don't know if anything is going to show up.

53
00:04:01,190 --> 00:04:07,430
For instance if I had a list of users something that we have done in the past and each of the users

54
00:04:07,430 --> 00:04:12,770
has certain number of photos but they don't know if they necessarily have photos or not.

55
00:04:12,770 --> 00:04:20,770
So I would have a photos or array of let's say you or I or not you images are strings.

56
00:04:20,960 --> 00:04:27,720
And then this would be actually an array and that is optional.

57
00:04:27,800 --> 00:04:32,980
I don't know if that thing actually is going to be mail or it is going to have IDs.

58
00:04:33,110 --> 00:04:34,410
So that's one thing.

59
00:04:34,580 --> 00:04:36,410
Well would they use non optional.

60
00:04:36,440 --> 00:04:37,040
Not optional.

61
00:04:37,070 --> 00:04:43,610
I will use it in situations where I know for a fact that that value exists and I know right off the

62
00:04:43,610 --> 00:04:44,330
beginning.

63
00:04:44,450 --> 00:04:51,770
So a situation like this where I'd have immediately said hey this value is going to be in this situation

64
00:04:52,100 --> 00:04:58,730
our i'm I've just started the OP that the moment I started I'm going to read the value from my user

65
00:04:59,450 --> 00:05:07,070
card username and then my username immediately is going to be our name of my youth.

66
00:05:07,280 --> 00:05:11,090
So in that situation we use none option.

67
00:05:11,390 --> 00:05:16,200
Finally we have something in of this caught on or off the street.

68
00:05:16,380 --> 00:05:23,000
Will talk about it in more detail in a minute but just look in the stand how it works is when you have

69
00:05:23,060 --> 00:05:24,520
optional listings.

70
00:05:24,650 --> 00:05:30,740
If I go in here and say preens option on this string is going to give me an error is going to say this

71
00:05:30,740 --> 00:05:32,250
is an optional string.

72
00:05:32,270 --> 00:05:34,610
So how do you want to convert it.

73
00:05:34,610 --> 00:05:37,080
Are you sure it exists like this.

74
00:05:37,160 --> 00:05:43,010
Or are you that find that either way a safe way to do it when you have an up to this train.

75
00:05:43,040 --> 00:05:45,710
And it is of that type whatever you want.

76
00:05:45,800 --> 00:05:47,230
Exclamation might.

77
00:05:47,320 --> 00:05:51,490
Then you can just place it in here because it will be treated right.

78
00:05:51,520 --> 00:05:56,940
This charade as if it was non optional.

79
00:05:57,200 --> 00:06:02,090
So once again this is not optional.

80
00:06:02,240 --> 00:06:06,200
Treated as if you know the value is always there.

81
00:06:06,400 --> 00:06:10,570
Up up this string is going to be treated as if it was not optional.

82
00:06:10,610 --> 00:06:20,330
It means its value is initialized immediately somehow the value for it is going to show up real soon.

83
00:06:20,330 --> 00:06:21,870
So with that in mind.

84
00:06:21,940 --> 00:06:24,650
And let me read you all these things a little bit.

85
00:06:24,680 --> 00:06:26,410
It's pretty nice.

86
00:06:26,750 --> 00:06:27,790
That's that.

87
00:06:27,800 --> 00:06:30,340
That's obviously not optional.

88
00:06:31,130 --> 00:06:37,640
This is the forced on dropped what happened.

89
00:06:38,040 --> 00:06:45,480
OK now let's see how are we going to go ahead about converting optionals into something that you know

90
00:06:45,480 --> 00:06:46,670
makes sense.

91
00:06:46,680 --> 00:06:54,190
So the first way is obviously force on writing for us on dropping which means we do something like print

92
00:06:55,260 --> 00:07:00,020
in the print I'm going to say optional this thing and I'm going to unwrap it.

93
00:07:00,060 --> 00:07:06,940
So whenever you have the sign of exclamation mark it's means on dropping either here are implicit on

94
00:07:06,950 --> 00:07:10,050
dropped or in here forced on rap of course.

95
00:07:10,090 --> 00:07:14,250
Maybe I can cut these and put them at the bottom because that's not what you're talking about.

96
00:07:14,260 --> 00:07:16,170
We're talking about this one the optional one.

97
00:07:16,440 --> 00:07:25,590
That's the force on rapping which is not safe not safe because you can't be sure if this value has something

98
00:07:25,590 --> 00:07:26,000
in it.

99
00:07:26,160 --> 00:07:31,740
As you can see in this situation right now I have the option of saying it is this thing but it is not

100
00:07:31,740 --> 00:07:34,140
going to be safe to force it.

101
00:07:34,140 --> 00:07:36,020
It doesn't have any value in it.

102
00:07:36,060 --> 00:07:37,310
It's not going to crash.

103
00:07:37,320 --> 00:07:40,330
It's going to give me a needle in this application.

104
00:07:40,470 --> 00:07:47,040
But in general if you have a value that value might very well you know be like the example I gave you

105
00:07:47,040 --> 00:07:49,230
were making it a logging in a user name.

106
00:07:49,230 --> 00:07:52,920
So we have to make sure that you know we don't force it.

107
00:07:52,920 --> 00:07:58,140
The other option is much better and much safer option is a conditional on wrapping.

108
00:07:58,230 --> 00:08:00,610
So how do we do conditional mapping.

109
00:08:00,750 --> 00:08:03,480
We have something called optional string.

110
00:08:03,810 --> 00:08:08,160
This is condition not conditional

111
00:08:10,560 --> 00:08:12,300
on mapping.

112
00:08:12,570 --> 00:08:14,920
So we have a value called optional history.

113
00:08:14,960 --> 00:08:25,950
I'm going to say if let's raise a string is optional a string then do something.

114
00:08:25,950 --> 00:08:30,830
So I'm essentially saying let's end oystering be that one.

115
00:08:30,930 --> 00:08:34,110
If it does exist do this.

116
00:08:34,190 --> 00:08:40,240
Print our value exists for instance.

117
00:08:40,600 --> 00:08:46,380
And then of course you can have an else which you decide about the other part of the other side if it

118
00:08:46,560 --> 00:08:48,410
doesn't exist.

119
00:08:48,420 --> 00:08:53,760
So if I run this application now obviously is going to crash in here because it's going to say you're

120
00:08:53,760 --> 00:08:55,990
forcing your value that is near.

121
00:08:56,100 --> 00:08:59,970
So I'm going to comment that out and then run the application.

122
00:09:01,870 --> 00:09:09,460
And then it says value doesn't exist simply because let's raise the string has never done anything in

123
00:09:09,610 --> 00:09:12,430
the optional string has always been known.

124
00:09:12,440 --> 00:09:14,260
Now let's do one other thing.

125
00:09:14,440 --> 00:09:17,020
What if you don't want to change this name.

126
00:09:17,080 --> 00:09:24,160
So you could actually do this you could say if left optional a string is optional a string then say

127
00:09:24,190 --> 00:09:29,440
value exist or let's just print the value.

128
00:09:29,440 --> 00:09:33,460
I'm going to say optional is saying the same thing in the else.

129
00:09:33,640 --> 00:09:42,370
Actually in the end I can't because you know I have to say it doesn't exist so again value doesn't exist.

130
00:09:42,520 --> 00:09:49,030
But what if somebody in the middle before we did this a statement I would have said optional testing

131
00:09:49,090 --> 00:09:57,410
is typing objective c.

132
00:09:57,410 --> 00:09:59,420
Now the value is going to be printed.

133
00:09:59,630 --> 00:10:06,370
So upshot is things you introduce them by a question mark at the end of them are optional values in

134
00:10:06,380 --> 00:10:11,420
general optional are integer optional flawed optional array whatever it is.

135
00:10:11,660 --> 00:10:16,100
If you want to force on them you just put an exclamation mark at the end of them.

136
00:10:16,130 --> 00:10:17,220
It is not safe.

137
00:10:17,240 --> 00:10:21,680
It is for when you're absolutely sure the value exists.

138
00:10:21,790 --> 00:10:30,380
Otherwise we use a conditional on wrapping to make sure that if flat the same name if you want if it

139
00:10:30,470 --> 00:10:37,370
equals to that and it doesn't become a value and there isn't that this thing will get a value is because

140
00:10:37,460 --> 00:10:40,800
actually string in here it's not optional.

141
00:10:40,910 --> 00:10:44,630
So I know I use the same name and it is becoming a little bit confusing.

142
00:10:44,690 --> 00:10:46,940
So I'm going to go with there is a string.

143
00:10:47,120 --> 00:10:54,510
I remember minor as a string doesn't have a question mark in front of it is not registering is in question.

144
00:10:54,590 --> 00:10:56,820
No it is a non optional.

145
00:10:56,840 --> 00:11:03,790
It's very similar to what we did here when we said that non optional is something I've said you feel

146
00:11:03,800 --> 00:11:05,620
that's that thing.

147
00:11:05,810 --> 00:11:11,360
It means that the value actually exists and then obviously based on that we can print it.

148
00:11:11,540 --> 00:11:17,890
And if it's not that thing if the else is happening we could decide what about for instance your luggin

149
00:11:17,930 --> 00:11:22,010
shouldn't happen because using name has been has not been provided.

150
00:11:22,580 --> 00:11:26,850
So there's finally another way of dropping an option.

151
00:11:26,900 --> 00:11:29,410
And that is using a type casting.

152
00:11:29,450 --> 00:11:36,350
So what I'm going to get myself a little bit of space in here and I'm going to say if I have an optional

153
00:11:36,410 --> 00:11:46,680
string I could do this I could say if option all this string as a string is not nil.

154
00:11:46,880 --> 00:11:49,520
And don't worry about the format of an IF statement.

155
00:11:49,520 --> 00:11:53,830
We will see that real soon in action in our next lesson which is about the control flow.

156
00:11:53,870 --> 00:12:00,300
So if that is not the case if it is not in there it's means we have a value.

157
00:12:00,530 --> 00:12:03,930
There is of course another easier way of doing this.

158
00:12:03,950 --> 00:12:06,790
Optional on dropping which is which is not as.

159
00:12:06,860 --> 00:12:14,680
As you know as funky as using an if let situation and that is this you could go in here and say if for

160
00:12:15,530 --> 00:12:20,900
optional string is not there then do whatever you want to do.

161
00:12:20,900 --> 00:12:24,580
For instance do this thing print that.

162
00:12:24,670 --> 00:12:30,530
I know this is not going to crash our application simply because it only happens if optional a string

163
00:12:30,530 --> 00:12:32,210
has a value in it.

164
00:12:32,240 --> 00:12:33,440
In this case it doesn't.

165
00:12:33,430 --> 00:12:35,750
So we know nothing is going to happen.

166
00:12:36,050 --> 00:12:42,090
So really quickly once again just to recover things that we discussed in this lesson.

167
00:12:42,290 --> 00:12:44,940
Actually I wouldn't mind if I rewrite everything.

168
00:12:44,940 --> 00:12:47,540
Real quick with an integer this time.

169
00:12:47,540 --> 00:12:52,930
So now I really have some some sort of understanding this should become a lot easier.

170
00:12:53,120 --> 00:13:01,060
So I'm going to say our my in and this might mean I'm going to give it a value right at the very beginning.

171
00:13:01,310 --> 00:13:04,880
And if you want I could actually say it is integer.

172
00:13:04,940 --> 00:13:12,530
So this is not optional and it means she is already so perfect.

173
00:13:12,530 --> 00:13:14,550
I'm going to have another one.

174
00:13:15,230 --> 00:13:18,250
Let's call it next.

175
00:13:18,390 --> 00:13:30,740
Integer is of that type integer and it's optional optional integer that needs to be on right before

176
00:13:30,740 --> 00:13:31,380
use.

177
00:13:31,550 --> 00:13:35,840
So we have to wrap this before we want to use it.

178
00:13:36,110 --> 00:13:49,850
And then finally there are on the integer which is integer and it has a little exclamation mark it is

179
00:13:50,000 --> 00:13:56,390
treated Azoff as if it was not optional.

180
00:13:56,900 --> 00:13:59,210
So let's see what do we do with all of these.

181
00:13:59,330 --> 00:14:08,600
If we go on to say print for instance to this thing are preens next integer next integer is an option

182
00:14:08,600 --> 00:14:11,180
or a value is going to crash this application.

183
00:14:11,180 --> 00:14:15,080
Now he's going to say you haven't really given me any value for it.

184
00:14:15,080 --> 00:14:17,100
I don't know if the value is nil.

185
00:14:17,240 --> 00:14:25,940
So you have to essentially give me a value if you try to force it say Hey I know it exists it's going

186
00:14:25,940 --> 00:14:29,790
to say well no it doesn't exist and it's going to crash application.

187
00:14:30,020 --> 00:14:31,640
So here's what you can do.

188
00:14:31,640 --> 00:14:40,520
You can say well if the next event is not net then do whatever you have to be doing.

189
00:14:40,700 --> 00:14:48,320
So that partially solves our problem a much better or more elegant solution for it is doing this saying

190
00:14:48,500 --> 00:15:03,190
if we let our next gain being next in us if that was let then do whatever you want to do then do what

191
00:15:03,230 --> 00:15:06,180
ever you want to do.

192
00:15:06,180 --> 00:15:07,890
All right.

193
00:15:08,310 --> 00:15:12,330
Otherwise it's still the net.

194
00:15:13,160 --> 00:15:15,460
So that's about that.

195
00:15:15,540 --> 00:15:20,310
I'm actually going to remove that because I want to write is for use we can have as a reference for

196
00:15:20,310 --> 00:15:21,190
future.

197
00:15:21,450 --> 00:15:27,480
So this will crash this one in here.

198
00:15:27,610 --> 00:15:37,680
Obviously it says what it is doing and you can do things such as this print on the top end and you don't

199
00:15:37,680 --> 00:15:39,190
need that exclamation mark.

200
00:15:39,190 --> 00:15:43,300
He's going to treat it as everything here reads

201
00:15:46,970 --> 00:15:49,560
as if it was not optional.

202
00:15:52,060 --> 00:15:59,090
All right you might actually keep this up as as like a cheat sheet for yourself.

203
00:15:59,440 --> 00:16:08,500
Non-optional us optional if it's Question My option is that of force on rap and it could potentially

204
00:16:08,500 --> 00:16:17,260
crash you have to use it and you are really really short and conditional on dropping some type of conditional

205
00:16:17,380 --> 00:16:21,920
on writing for when you are sure this thing actually.

206
00:16:22,050 --> 00:16:27,770
For it makes sure for you that this thing exists thing you say let something to be that thing.

207
00:16:28,000 --> 00:16:35,290
And finally when you want to use your implicitly arm dropped valueless now a more pressing question

208
00:16:35,290 --> 00:16:38,530
for you might be so where do I use either of these.

209
00:16:38,620 --> 00:16:42,810
Obviously use not option us for and you know what your value is.

210
00:16:42,810 --> 00:16:48,700
Almost immediately you use your optionals for when you don't know what is going to be there or is it

211
00:16:48,700 --> 00:16:49,850
going to be there.

212
00:16:50,030 --> 00:16:58,020
And you use your implicitly on dropped values for situations where there are value for that value but

213
00:16:58,030 --> 00:17:03,100
is going to be there or is going to be populated almost immediately.

214
00:17:03,100 --> 00:17:11,830
So you're reading a user your user most definitely has in name and has a unique ID and then you own

215
00:17:11,920 --> 00:17:13,330
your unique ID.

216
00:17:13,370 --> 00:17:22,240
You state as to be a and implicitly on up to one because it is not there now but it will be immediately.

217
00:17:22,240 --> 00:17:23,920
So that's about that.

218
00:17:23,920 --> 00:17:28,080
With all of that in mind Arved Let's move on to our next lesson.

219
00:17:28,090 --> 00:17:33,820
I have included a link to the optionals from the swift documentation.

220
00:17:34,030 --> 00:17:38,240
I would recommend you read through that as well because that really helps as well.
