1 00:00:00,450 --> 00:00:06,960 Here's an important question when you're coding stuff, should you use null or nil when you initialize 2 00:00:06,960 --> 00:00:07,500 a variable? 3 00:00:08,490 --> 00:00:10,810 Well, if you're new to coding, you don't know what that means. 4 00:00:10,830 --> 00:00:15,840 Now, Lonell means there's literally nothing inside the variable, right? 5 00:00:15,840 --> 00:00:18,900 So you have a bit of memory space or no memory space. 6 00:00:18,900 --> 00:00:20,130 You have an address for the memory. 7 00:00:20,430 --> 00:00:22,620 And when you query it, there is no variable. 8 00:00:22,620 --> 00:00:23,640 There's literally nothing there. 9 00:00:24,450 --> 00:00:30,780 So if I was going to try and do that in Swift, I would use the magic word now, which means there's 10 00:00:30,780 --> 00:00:31,380 nothing in it. 11 00:00:31,890 --> 00:00:38,010 Now, there may be an issue with this because Knill requires a contextual type, i.e., you can't just 12 00:00:38,010 --> 00:00:39,270 put nothing in a variable. 13 00:00:39,420 --> 00:00:41,330 It goes, hey, what's wrong with you? 14 00:00:41,340 --> 00:00:42,290 Why are you trying to do that? 15 00:00:42,570 --> 00:00:45,150 In fact, if we put. 16 00:00:46,190 --> 00:00:47,660 VAR scorelines. 17 00:00:48,830 --> 00:00:56,020 Because most people think that's the solution, Neal is not an integer, zero is, but nil is not OK. 18 00:00:56,030 --> 00:00:59,780 So the question is, how do you store nail in a variable? 19 00:00:59,780 --> 00:01:02,990 Because there are times when you will actually need to do that. 20 00:01:03,470 --> 00:01:08,150 If you're from the C shop world, this is the equivalent to now, if you're from Scotland, I think 21 00:01:08,150 --> 00:01:10,630 it's no, I can't remember anyway. 22 00:01:11,360 --> 00:01:12,470 So how do we get around this? 23 00:01:12,500 --> 00:01:15,710 Well, we get around this by having what's called an optional. 24 00:01:16,100 --> 00:01:21,850 Now, optional are a very elegant way of handling the now or nil problem. 25 00:01:22,230 --> 00:01:25,580 So let me just check my notes and see what I've got prepared for you here. 26 00:01:25,670 --> 00:01:26,960 Yeah, I know what it is. 27 00:01:27,780 --> 00:01:32,930 The way we tell Swift that this is optional is we put a question mark on it. 28 00:01:33,560 --> 00:01:38,420 So it says this is a box that could contain an integer or could contain nothing. 29 00:01:38,900 --> 00:01:42,200 So when we query, it will get either of those results. 30 00:01:42,980 --> 00:01:49,070 Now, the reason that we have options is because in any software, if you try to access and no value 31 00:01:49,070 --> 00:01:56,360 or no value and you try to manipulate that value, expecting, for example, an integer, then the whole 32 00:01:56,360 --> 00:02:03,350 program will crash because it's a no no reference error or an exception error or something similar like 33 00:02:03,350 --> 00:02:03,590 that. 34 00:02:05,230 --> 00:02:07,630 OK, so where do we move from here? 35 00:02:07,960 --> 00:02:15,670 Well, we can also reassign this score to an actual integer, so now we have score equals one. 36 00:02:17,230 --> 00:02:22,600 So the first case we have nothing, the second case, we have something, how do we get the score? 37 00:02:22,750 --> 00:02:25,910 So if I say print score, what happens? 38 00:02:26,290 --> 00:02:28,690 Well, I'm going to guess that there'll be an. 39 00:02:29,660 --> 00:02:35,030 Well, a slight error, this is a warning, not an error expression implicitly coerced from into any. 40 00:02:35,480 --> 00:02:40,160 This is just Swift's way of getting over the error that I'm introducing here. 41 00:02:40,310 --> 00:02:43,220 OK, so it's not a major error, but it is an error nonetheless. 42 00:02:43,580 --> 00:02:50,450 A much better way to do this is to first check if score is not equal to nil. 43 00:02:53,100 --> 00:02:57,780 So it's going to dive into that variable check, if it's nail, if it isn't, then we have access to 44 00:02:57,780 --> 00:03:00,820 the variable and we can then print the score. 45 00:03:01,590 --> 00:03:03,480 Now, let's see if this gives us an error. 46 00:03:03,480 --> 00:03:08,490 We get the same error, but because we've checked it, we can put an exclamation after it. 47 00:03:09,270 --> 00:03:15,450 And that's telling Swift and iOS that there is definitely an integer in here. 48 00:03:15,450 --> 00:03:16,800 I know because I've checked it. 49 00:03:17,040 --> 00:03:20,920 And I want you just to assume that is correct. 50 00:03:21,240 --> 00:03:24,890 Now, if you get this bit wrong, that's when you're going to get a crash. 51 00:03:24,900 --> 00:03:27,700 So make sure you have actually checked it. 52 00:03:29,310 --> 00:03:31,440 OK, what else can we do with auctioneer's? 53 00:03:32,770 --> 00:03:41,680 Well, we can assign if let's see equal score, we can assign a variable to that optional. 54 00:03:42,600 --> 00:03:49,950 OK, so S.E. is equal to score, if there's an integer in it, then we can print Southsea or we can 55 00:03:49,950 --> 00:03:55,800 do whatever we need to with SC and in that case, we don't have to put the exclamation mark. 56 00:03:57,390 --> 00:04:04,050 Okay, and I like the syntax because it makes it a bit more clear compared to this syntax, right? 57 00:04:04,200 --> 00:04:07,470 Because this one, this is called force unwrapping. 58 00:04:07,770 --> 00:04:10,020 We're unwrapping the variable forcefully. 59 00:04:10,020 --> 00:04:14,490 We're not giving it a chance to gracefully decline and say there's nothing in here. 60 00:04:15,300 --> 00:04:18,990 This, to me, feels a lot more safe than the previous version. 61 00:04:20,580 --> 00:04:23,240 OK, so we've got now a little bit of homework for you. 62 00:04:27,680 --> 00:04:33,110 Write your own optional integer of your age and then print it out if it exists. 63 00:04:33,440 --> 00:04:35,120 So I've given you a couple of templates there. 64 00:04:35,600 --> 00:04:36,970 Go ahead and try that out.