1 00:00:00,540 --> 00:00:10,080 So let's discover what variables are using an Xcode playground to open up Xcode, go to file new playground. 2 00:00:10,320 --> 00:00:15,240 Now playgrounds just let you mess around with basic code and give you the results instantly so you don't 3 00:00:15,240 --> 00:00:18,780 have to create a brand new iOS project or anything crazy like that. 4 00:00:19,440 --> 00:00:22,740 Hit next on blank and save it wherever you want to save it. 5 00:00:23,400 --> 00:00:25,350 So mine's going straight to the desktop. 6 00:00:25,890 --> 00:00:27,470 Now, let me resize this for you. 7 00:00:29,330 --> 00:00:33,750 And you'll see that Xcode has pretty populated some stuff in here. 8 00:00:34,550 --> 00:00:39,040 First of all, it's imported UI kit, which allows us to run certain things in the code. 9 00:00:39,380 --> 00:00:40,980 Don't worry about that for now. 10 00:00:41,000 --> 00:00:43,510 Imports will be part of the course as we go along. 11 00:00:44,710 --> 00:00:53,680 What I want to focus on is this thing vare SDR, VA stands for variable and all the variable is at its 12 00:00:53,680 --> 00:00:57,280 basic level is something that's stored in memory. 13 00:00:57,820 --> 00:01:02,850 In fact, everything in your program is technically a variable if you use that definition. 14 00:01:03,280 --> 00:01:07,360 So functions are stored in memory classes and objects are stored in memory. 15 00:01:07,720 --> 00:01:10,780 So they're technically variables, at least in my opinion. 16 00:01:12,370 --> 00:01:19,510 The next section is the name of the variable, so in this case, star, now SDR is an awful name for 17 00:01:19,510 --> 00:01:24,990 a variable because if I come back to this code in six months, I don't know what Star stands for. 18 00:01:25,150 --> 00:01:25,600 Do I? 19 00:01:25,690 --> 00:01:26,440 Neither do you. 20 00:01:27,050 --> 00:01:33,580 If I come to your code and I see that I'm liable to not want to help you with your code because nothing 21 00:01:33,580 --> 00:01:34,190 is obvious. 22 00:01:34,450 --> 00:01:38,440 So if I was going to give this a better name, I would call it something like welcome message. 23 00:01:38,770 --> 00:01:41,320 And then everyone knows what it is. 24 00:01:42,130 --> 00:01:43,570 But I'm getting ahead of myself there. 25 00:01:44,350 --> 00:01:50,440 I do, however, like to have students trained in naming things correctly so their software and code 26 00:01:50,440 --> 00:01:54,130 is readable anyway, back to the subject at hand. 27 00:01:54,130 --> 00:02:03,700 So we have a variable string or star, which is this thing surrounded by quotes and that is a string. 28 00:02:04,030 --> 00:02:07,870 A string is simply a collection of characters in order. 29 00:02:09,460 --> 00:02:11,150 So that is simple enough. 30 00:02:11,440 --> 00:02:12,960 Now where do we go from here? 31 00:02:15,620 --> 00:02:20,810 Well, what we can do, you see this little blue icon at the side, if you hover over it, if you hit 32 00:02:20,810 --> 00:02:24,230 play on that, you will see over on the right hand side. 33 00:02:25,790 --> 00:02:32,210 The result of what you're doing there now, this is currently running and now it displays hello playground. 34 00:02:32,570 --> 00:02:38,000 These things are much faster to run the second time because it's already compiled a lot of stuff in 35 00:02:38,000 --> 00:02:38,630 the background. 36 00:02:39,500 --> 00:02:41,550 So that just spits out our variable. 37 00:02:41,780 --> 00:02:47,090 Now, what if I want to spit out this variable in the console and I'll show you what that means in a 38 00:02:47,090 --> 00:02:47,570 second. 39 00:02:48,230 --> 00:02:50,540 But we're going to run something called a print. 40 00:02:51,500 --> 00:02:57,380 And if I press play on that, look what pops up at the bottom of the screen, we have Hello Playground. 41 00:02:59,130 --> 00:03:05,130 So that print statement prints things out for us, and you can use that to debug your programs as you 42 00:03:05,130 --> 00:03:11,760 get more advanced, so you'll print out this method isn't working or this error came back from the server 43 00:03:11,760 --> 00:03:16,260 or whatever it is, and the user doesn't see it, but use the developer will be able to see it. 44 00:03:17,010 --> 00:03:17,370 Right. 45 00:03:18,480 --> 00:03:19,920 Let's create another string. 46 00:03:19,920 --> 00:03:23,790 So var my let's create another string with an awful name. 47 00:03:24,630 --> 00:03:30,780 My string is equal to what let's call this ground. 48 00:03:31,230 --> 00:03:34,210 So actually a better name would be name, but we'll leave it as it is. 49 00:03:34,590 --> 00:03:41,430 Now how does Swift and iOS and Xcode and all the rest of it know that this is a string while we've put 50 00:03:41,430 --> 00:03:42,630 it between quotes. 51 00:03:43,230 --> 00:03:44,430 So that's how it knows. 52 00:03:45,060 --> 00:03:47,160 But we can tell it explicitly. 53 00:03:49,460 --> 00:03:55,460 By putting a colon and then the word string with a capital S. So that tells it this is a string and 54 00:03:55,460 --> 00:04:01,610 if I start typing that again, you look at that little handy hint that pops up string a Unicode string 55 00:04:01,610 --> 00:04:04,820 value, that is a collection of characters, like I said. 56 00:04:05,720 --> 00:04:10,530 OK, so you can explicitly tell it so there are two ways of declaring variables, really. 57 00:04:11,000 --> 00:04:15,980 One is implicit let the system guess what it is and the other one is explicit. 58 00:04:16,340 --> 00:04:19,620 Tell the system what it is you'll get used to. 59 00:04:19,640 --> 00:04:21,230 Which one to use as in when? 60 00:04:21,230 --> 00:04:22,250 Because it varies. 61 00:04:22,260 --> 00:04:22,600 Right. 62 00:04:23,270 --> 00:04:26,450 And I know lots of programmers who just let the system guess the whole time. 63 00:04:26,660 --> 00:04:28,490 I would say that decent programmers as well. 64 00:04:29,500 --> 00:04:32,920 OK, now, of course, we can do the same here, we can print my string. 65 00:04:34,030 --> 00:04:39,580 And if we run this whole lot, we should get what we need now, notice when you print my string, we 66 00:04:39,580 --> 00:04:42,830 get this backslash in at the end of my name there. 67 00:04:43,510 --> 00:04:44,290 Why is that? 68 00:04:44,620 --> 00:04:50,680 Well, that's the short code for a new line, what you call it, carriage return a break. 69 00:04:51,370 --> 00:04:52,810 So it goes to another line. 70 00:04:54,150 --> 00:04:59,480 That doesn't actually get printed out, as you can see down here in the console, you just get the word, 71 00:04:59,790 --> 00:05:04,890 but the new line is printed because the next print statement happens on a new line. 72 00:05:06,150 --> 00:05:10,650 Right, so that's how we implicitly and explicitly declare a variable. 73 00:05:11,580 --> 00:05:13,500 Now, why is it called a variable, do you think? 74 00:05:14,610 --> 00:05:20,700 It's not constant, it's not called the constant, there are constants, but not in this case variable, 75 00:05:20,700 --> 00:05:22,220 because it is variable. 76 00:05:22,800 --> 00:05:24,180 So I can change my name. 77 00:05:26,910 --> 00:05:27,300 To. 78 00:05:28,930 --> 00:05:36,610 Mike, because we can wipe whatever's in that memory space and put something new in it and allocate 79 00:05:36,610 --> 00:05:40,530 more memory if need be, I'm not sure we can allocate less memory. 80 00:05:40,540 --> 00:05:44,960 So I guess that's something to watch out for, right. 81 00:05:44,980 --> 00:05:47,110 So variables are precisely that. 82 00:05:47,110 --> 00:05:47,990 They are variable. 83 00:05:48,440 --> 00:05:50,920 Now, what are we going to do next? 84 00:05:51,550 --> 00:05:55,240 Well, let's look at the final category in here, which is the constant. 85 00:05:55,480 --> 00:06:01,810 Now, in lots of languages, we use the word const or perhaps even constant to define something that 86 00:06:01,810 --> 00:06:03,130 cannot change. 87 00:06:03,520 --> 00:06:08,860 So if I wanted my name to be set in stone, I would set it as constant, unchangeable. 88 00:06:09,370 --> 00:06:11,830 The way we do that and Swift is let. 89 00:06:13,300 --> 00:06:15,370 My name is Quilty. 90 00:06:17,400 --> 00:06:18,570 Can't even spell my own name. 91 00:06:19,540 --> 00:06:20,670 Grant, right? 92 00:06:21,340 --> 00:06:26,830 So that's perfectly fine, that will run, but if I run, my name is equal to Mike. 93 00:06:27,350 --> 00:06:28,340 Watch what happens. 94 00:06:28,750 --> 00:06:30,220 Just wait for the system to catch up. 95 00:06:30,850 --> 00:06:32,150 Cannot assigned to value. 96 00:06:32,170 --> 00:06:34,000 My name is a let constant. 97 00:06:34,330 --> 00:06:35,770 Right, because it is constant. 98 00:06:37,820 --> 00:06:43,760 Something of note for everyone, beginning programming their programming journey is wherever you can 99 00:06:44,210 --> 00:06:45,790 use a constant right. 100 00:06:45,800 --> 00:06:54,260 It's really good practice to keep things constant and to not modify variables on the fly unless you 101 00:06:54,260 --> 00:06:58,790 have to, because you do have to on occasion, in fact, maybe half the time. 102 00:06:59,870 --> 00:07:04,970 But constants mean that you don't have what we call spaghetti code crossing over each other. 103 00:07:05,210 --> 00:07:06,920 Everything's changing everything else. 104 00:07:07,220 --> 00:07:10,070 And then when you get a bug, it's really hard to track down. 105 00:07:10,250 --> 00:07:13,200 So constants are your friend, right? 106 00:07:13,280 --> 00:07:16,800 I'm going to set you guys a little bit of homework here. 107 00:07:17,210 --> 00:07:17,960 Now, what is it? 108 00:07:18,020 --> 00:07:19,100 I've got my notes here. 109 00:07:20,570 --> 00:07:27,030 Create a string variable with your name in it and print that string to the command line. 110 00:07:27,290 --> 00:07:30,930 So I want you to create a string that assigns your name to it. 111 00:07:30,950 --> 00:07:32,600 It can either be variable or constant. 112 00:07:32,750 --> 00:07:34,870 And then I want you to print that the command line. 113 00:07:35,510 --> 00:07:36,050 Off you go.