1 00:00:00,270 --> 00:00:03,210 ‫-: Hi. Following this lecture, 2 00:00:03,210 --> 00:00:08,070 ‫we are going to start learning about the Swift basics. 3 00:00:08,070 --> 00:00:09,630 ‫And in order to do that, 4 00:00:09,630 --> 00:00:13,020 ‫I actually need something very simple. 5 00:00:13,020 --> 00:00:16,110 ‫I don't need any main.storyboards 6 00:00:16,110 --> 00:00:18,660 ‫I don't need any simulators 7 00:00:18,660 --> 00:00:22,170 ‫because I won't be building any user interfaces. 8 00:00:22,170 --> 00:00:25,890 ‫I won't be just displaying those user interfaces 9 00:00:25,890 --> 00:00:27,360 ‫in my simulator. 10 00:00:27,360 --> 00:00:32,360 ‫I just need a very basic way to write Swift code 11 00:00:32,730 --> 00:00:34,530 ‫and see the outputs 12 00:00:34,530 --> 00:00:38,670 ‫without giving too much thought into user interface 13 00:00:38,670 --> 00:00:42,870 ‫or without having to run simulator every time 14 00:00:42,870 --> 00:00:45,180 ‫and wait for it to compile 15 00:00:45,180 --> 00:00:48,243 ‫and build and show me the user face and stuff. 16 00:00:49,470 --> 00:00:53,490 ‫And in order to do that, I'm gonna show you two options. 17 00:00:53,490 --> 00:00:56,100 ‫And don't worry, after this section, 18 00:00:56,100 --> 00:00:58,890 ‫after we complete the basics of Swift, 19 00:00:58,890 --> 00:01:01,140 ‫we are going to go back to that as well. 20 00:01:01,140 --> 00:01:03,420 ‫We're going to create user interfaces, 21 00:01:03,420 --> 00:01:05,610 ‫we're going to build apps, 22 00:01:05,610 --> 00:01:08,820 ‫we're going to test them in the simulators, 23 00:01:08,820 --> 00:01:12,900 ‫All the course will be based on this practice. 24 00:01:12,900 --> 00:01:14,400 ‫However, in this section, 25 00:01:14,400 --> 00:01:17,820 ‫in the basic section, we don't need that. 26 00:01:17,820 --> 00:01:21,090 ‫We just need a simple method, 27 00:01:21,090 --> 00:01:26,090 ‫simple technique, to write our code and see the outputs. 28 00:01:26,190 --> 00:01:29,520 ‫First one is to create a Playground, 29 00:01:29,520 --> 00:01:32,910 ‫and that is the common solution 30 00:01:32,910 --> 00:01:37,110 ‫in order to learn about basic Swift operations. 31 00:01:37,110 --> 00:01:40,800 ‫So as you can see, we can explore new ideas 32 00:01:40,800 --> 00:01:44,400 ‫quickly and easily in the Playground. 33 00:01:44,400 --> 00:01:45,720 ‫So when you click on this, 34 00:01:45,720 --> 00:01:48,300 ‫it will ask you for a template again, 35 00:01:48,300 --> 00:01:50,580 ‫and of course we are going to go for "Blank" 36 00:01:50,580 --> 00:01:54,780 ‫because we are going to create everything from scratch, 37 00:01:54,780 --> 00:01:58,890 ‫and you can call your Playground whatever you want. 38 00:01:58,890 --> 00:02:01,620 ‫Okay? I'm going to call it "Variables." 39 00:02:01,620 --> 00:02:04,140 ‫This will be our first topic, 40 00:02:04,140 --> 00:02:07,890 ‫and we are going to see what a variable is, 41 00:02:07,890 --> 00:02:10,282 ‫and we are going to create this 42 00:02:10,282 --> 00:02:14,040 ‫and save it in whenever, wherever I want. 43 00:02:14,040 --> 00:02:17,670 ‫I'm going to go for desktop or I'm going to go 44 00:02:17,670 --> 00:02:20,880 ‫for iOS "Complete" folder, in fact. 45 00:02:20,880 --> 00:02:24,300 ‫And you can just choose it over here as well. 46 00:02:24,300 --> 00:02:28,230 ‫I'm going to save every Playground that we are creating 47 00:02:28,230 --> 00:02:30,180 ‫in my iOS "Complete" folder. 48 00:02:30,180 --> 00:02:32,880 ‫And later on, I'm just going to share all 49 00:02:32,880 --> 00:02:34,380 ‫of those things for you. 50 00:02:34,380 --> 00:02:37,470 ‫So let me create, actually, a new "Playgrounds" folder 51 00:02:37,470 --> 00:02:41,850 ‫and save this Playgrounds in my "Playgrounds" folder so 52 00:02:41,850 --> 00:02:45,690 ‫that I can zip them and share with them easily. 53 00:02:45,690 --> 00:02:47,010 ‫So here we go. 54 00:02:47,010 --> 00:02:50,070 ‫When you first create the Playground, 55 00:02:50,070 --> 00:02:52,110 ‫it shows you something like this. 56 00:02:52,110 --> 00:02:55,410 ‫Okay? So you're importing something. 57 00:02:55,410 --> 00:02:58,560 ‫Importing means you're importing a library 58 00:02:58,560 --> 00:03:00,480 ‫into this project 59 00:03:00,480 --> 00:03:04,470 ‫and you are running something called "str" 60 00:03:04,470 --> 00:03:06,540 ‫so this is a variable, actually. 61 00:03:06,540 --> 00:03:09,900 ‫And where is the keyword to define a variable? 62 00:03:09,900 --> 00:03:12,300 ‫We will see what it is later on. 63 00:03:12,300 --> 00:03:15,210 ‫If you click on this play button over here, 64 00:03:15,210 --> 00:03:17,880 ‫it will run this Playground. 65 00:03:17,880 --> 00:03:20,070 ‫If you're doing this for the first time, 66 00:03:20,070 --> 00:03:23,227 ‫and I believe this is my first time on Xcode 11 67 00:03:23,227 --> 00:03:27,380 ‫for Playgrounds, it's gonna take some time. 68 00:03:27,380 --> 00:03:31,260 ‫So the most basic thing about the Playgrounds: 69 00:03:31,260 --> 00:03:34,020 ‫they have to be fast and they have to be efficient, 70 00:03:34,020 --> 00:03:36,720 ‫because we are just writing some code, 71 00:03:36,720 --> 00:03:41,640 ‫and we are expecting to see the outcome of these codes fast. 72 00:03:43,110 --> 00:03:46,500 ‫If you get stuck in Playgrounds, 73 00:03:46,500 --> 00:03:50,430 ‫if this doesn't work for some reason, 74 00:03:50,430 --> 00:03:52,290 ‫I will show you another option, 75 00:03:52,290 --> 00:03:57,290 ‫because sometimes students experience some difficulties 76 00:03:57,390 --> 00:03:58,320 ‫with the Playground. 77 00:03:58,320 --> 00:03:59,610 ‫It should be like this. 78 00:03:59,610 --> 00:04:01,740 ‫You have to see the output 79 00:04:01,740 --> 00:04:05,040 ‫on the right hand side of this Playground. 80 00:04:05,040 --> 00:04:08,575 ‫So if you don't see it, you can try to close this Playground 81 00:04:08,575 --> 00:04:13,476 ‫and open it again and hitting the play button one more time, 82 00:04:13,476 --> 00:04:18,476 ‫because it should display these variables 83 00:04:18,840 --> 00:04:20,490 ‫very fast actually. 84 00:04:20,490 --> 00:04:22,260 ‫So let me create another one, 85 00:04:22,260 --> 00:04:25,092 ‫so you can just write it like me, 86 00:04:25,092 --> 00:04:27,690 ‫and I'm going to explain what is a word, 87 00:04:27,690 --> 00:04:30,030 ‫what is a print later on. 88 00:04:30,030 --> 00:04:33,360 ‫But just for right now, know that I'm creating a variable 89 00:04:33,360 --> 00:04:35,910 ‫and I'm trying to print it. 90 00:04:35,910 --> 00:04:38,040 ‫So if I click the play button 91 00:04:38,040 --> 00:04:41,850 ‫I will see the result on on the right hand side. 92 00:04:41,850 --> 00:04:45,180 ‫And since I am printing this, I will see the result 93 00:04:45,180 --> 00:04:49,350 ‫on the lower side of this Playground as well. 94 00:04:49,350 --> 00:04:50,880 ‫So this is the print section, 95 00:04:50,880 --> 00:04:55,178 ‫and right hand side is where we get the output 96 00:04:55,178 --> 00:04:58,080 ‫of our code, actually. 97 00:04:58,080 --> 00:05:00,300 ‫So this is the logic of Playground. 98 00:05:00,300 --> 00:05:01,980 ‫So you write some codes 99 00:05:01,980 --> 00:05:04,800 ‫and you see the results on the right hand site. 100 00:05:04,800 --> 00:05:09,720 ‫For example, I multiply 10 times five, 101 00:05:09,720 --> 00:05:14,670 ‫and I can see the 50 instantaneously on the right hand side. 102 00:05:14,670 --> 00:05:18,210 ‫So if this is working out for you, that's great. 103 00:05:18,210 --> 00:05:21,780 ‫So you don't have to go for the second option at all, 104 00:05:21,780 --> 00:05:24,300 ‫because Playground is actually better. 105 00:05:24,300 --> 00:05:27,030 ‫And I'm going to explain everything 106 00:05:27,030 --> 00:05:29,100 ‫in the Playgrounds as well. 107 00:05:29,100 --> 00:05:34,100 ‫But I have come across some situations students could not 108 00:05:34,350 --> 00:05:37,680 ‫start Playgrounds, whatever they try, okay? 109 00:05:37,680 --> 00:05:41,910 ‫So they try to close this, they try to restart 110 00:05:41,910 --> 00:05:44,610 ‫their computers, they try to delete the Playground 111 00:05:44,610 --> 00:05:48,030 ‫and create it again, but it won't start. 112 00:05:48,030 --> 00:05:51,300 ‫So if you come across a situation like this, 113 00:05:51,300 --> 00:05:53,613 ‫I'm going to show you a second way. 114 00:05:55,170 --> 00:05:58,080 ‫And let me show you the second way. 115 00:05:58,080 --> 00:06:00,330 ‫Let me go over here to Xcode 116 00:06:00,330 --> 00:06:03,660 ‫and say "New project" rather than Playground, 117 00:06:03,660 --> 00:06:07,423 ‫I'm going to go for a project, but rather than iOS, 118 00:06:07,423 --> 00:06:11,870 ‫now I'm going to create something for Mac OS. 119 00:06:11,870 --> 00:06:14,790 ‫I'm not, of course, going to build an app 120 00:06:14,790 --> 00:06:17,819 ‫for Mac OS in this at this moment, 121 00:06:17,819 --> 00:06:21,540 ‫but I'm going to create a very simple project 122 00:06:21,540 --> 00:06:23,250 ‫in which we can write code 123 00:06:23,250 --> 00:06:26,370 ‫and show the output instantaneously again, 124 00:06:26,370 --> 00:06:30,780 ‫without having to deal with the main storyboard, 125 00:06:30,780 --> 00:06:33,660 ‫or deal with the simulator at all. 126 00:06:33,660 --> 00:06:35,940 ‫So if you choose the Mac OS 127 00:06:35,940 --> 00:06:40,290 ‫you can come here, choose "Command Line Tool," okay? 128 00:06:40,290 --> 00:06:42,570 ‫That's what we are going to create here. 129 00:06:42,570 --> 00:06:45,300 ‫And we have to give you the product name one more time. 130 00:06:45,300 --> 00:06:49,290 ‫So I'm going to say "CommandLineVariables" over here, 131 00:06:49,290 --> 00:06:51,780 ‫but I'm not going to write any code in here. 132 00:06:51,780 --> 00:06:55,830 ‫I'm not going to explain all the section 133 00:06:55,830 --> 00:06:57,810 ‫in command variables, 134 00:06:57,810 --> 00:07:00,510 ‫but I'm just going to show you what it is. 135 00:07:00,510 --> 00:07:02,550 ‫So here you can choose Swift, 136 00:07:02,550 --> 00:07:05,820 ‫and as you can see, we can write C or C++ code 137 00:07:05,820 --> 00:07:09,180 ‫in here as well, but we are not gonna do that. 138 00:07:09,180 --> 00:07:11,070 ‫But rather, we are just going to go 139 00:07:11,070 --> 00:07:14,460 ‫for Swift and create our project. 140 00:07:14,460 --> 00:07:15,510 ‫And as you can see, 141 00:07:15,510 --> 00:07:19,080 ‫this creates a project just like as we did before. 142 00:07:19,080 --> 00:07:22,380 ‫But rather than having a view controller.swift 143 00:07:22,380 --> 00:07:24,480 ‫or app delegate or scene delegate, 144 00:07:24,480 --> 00:07:28,200 ‫we only have something called "main.swift." 145 00:07:28,200 --> 00:07:32,610 ‫So this is a basic Swift file in which we can write code, 146 00:07:32,610 --> 00:07:37,610 ‫and when we play this, when we run this here using my Mac, 147 00:07:38,370 --> 00:07:42,780 ‫it will just run the code and display the results for me. 148 00:07:42,780 --> 00:07:46,050 ‫For some reason, I'm getting an error message. 149 00:07:46,050 --> 00:07:47,820 ‫Let me see. 150 00:07:47,820 --> 00:07:50,370 ‫Yeah, as you can see, I'm not connected 151 00:07:50,370 --> 00:07:54,360 ‫to my Apple ID, even though I suggested you to go 152 00:07:54,360 --> 00:07:58,774 ‫and do this in your signing and capability section. 153 00:07:58,774 --> 00:08:01,950 ‫Apparently, I didn't do that. 154 00:08:01,950 --> 00:08:05,640 ‫So let me go to signing and capabilities. 155 00:08:05,640 --> 00:08:07,080 ‫And here you go. 156 00:08:07,080 --> 00:08:09,210 ‫It doesn't see my account here, 157 00:08:09,210 --> 00:08:12,480 ‫so it doesn't see my see my Apple ID. 158 00:08:12,480 --> 00:08:17,370 ‫So this is very easy to fix if it's not shown, 159 00:08:17,370 --> 00:08:20,490 ‫if it's not chosen for you, just choose it 160 00:08:20,490 --> 00:08:22,170 ‫and say, "sign in." 161 00:08:22,170 --> 00:08:26,850 ‫And then later on you can just give your Apple ID password 162 00:08:26,850 --> 00:08:31,620 ‫over here so that it can connect you to your Xcode project. 163 00:08:31,620 --> 00:08:33,900 ‫And I believe this is not an issue for you, 164 00:08:33,900 --> 00:08:38,220 ‫because you have done this in the previous section. 165 00:08:38,220 --> 00:08:40,200 ‫For right now, it's an issue for me. 166 00:08:40,200 --> 00:08:43,293 ‫So I'm signing in with my Apple ID 167 00:08:43,293 --> 00:08:48,293 ‫and it's going to assign my Apple ID to Xcode project 168 00:08:48,360 --> 00:08:50,523 ‫and this will be okay then. 169 00:08:51,480 --> 00:08:54,480 ‫So right now I'm here, signed in. 170 00:08:54,480 --> 00:08:57,360 ‫Let me to my main.swift 171 00:08:57,360 --> 00:09:00,240 ‫and try to run this one more time, 172 00:09:00,240 --> 00:09:02,280 ‫and the errors will go away. 173 00:09:02,280 --> 00:09:06,810 ‫And as you can see, now I will get the log. 174 00:09:06,810 --> 00:09:10,980 ‫I will see the output of this print. 175 00:09:10,980 --> 00:09:11,813 ‫As you can see, 176 00:09:11,813 --> 00:09:16,800 ‫I see the output over here on the right hand side, bottom. 177 00:09:16,800 --> 00:09:19,290 ‫So if I do something like this, 178 00:09:19,290 --> 00:09:22,740 ‫now I have to print this rather than seeing 179 00:09:22,740 --> 00:09:23,970 ‫on the right hand side, 180 00:09:23,970 --> 00:09:27,510 ‫because right now we don't have that kind 181 00:09:27,510 --> 00:09:29,760 ‫of capability in here. 182 00:09:29,760 --> 00:09:32,700 ‫So I'm going to wrap this around a print. 183 00:09:32,700 --> 00:09:33,690 ‫And if I do that, 184 00:09:33,690 --> 00:09:38,040 ‫it will just print the result for me in here like this. 185 00:09:38,040 --> 00:09:42,930 ‫So you can use this technique or you can use the Playground. 186 00:09:42,930 --> 00:09:45,923 ‫And I suggest if it is working for you, 187 00:09:45,923 --> 00:09:49,620 ‫you use the Playground, because it's simpler. 188 00:09:49,620 --> 00:09:53,190 ‫You don't have to write "print" every time 189 00:09:53,190 --> 00:09:55,501 ‫in order to see the results. 190 00:09:55,501 --> 00:09:59,160 ‫Okay? So you can create new variables 191 00:09:59,160 --> 00:10:00,990 ‫like this in here as well. 192 00:10:00,990 --> 00:10:02,610 ‫You can see something like this. 193 00:10:02,610 --> 00:10:05,550 ‫You can say where my number is four times four 194 00:10:05,550 --> 00:10:07,110 ‫and print (mynumber). 195 00:10:07,110 --> 00:10:11,430 ‫And if you just run this, you will see the same results. 196 00:10:11,430 --> 00:10:13,950 ‫So we are seeing 20 in here, 197 00:10:13,950 --> 00:10:17,100 ‫and we will see the same thing in here as well, 198 00:10:17,100 --> 00:10:18,870 ‫if we run this. 199 00:10:18,870 --> 00:10:22,680 ‫Okay? As you can see, we see 16 200 00:10:22,680 --> 00:10:27,510 ‫because, yeah, I said four times four, but it's working. 201 00:10:27,510 --> 00:10:29,910 ‫So choose one of them. 202 00:10:29,910 --> 00:10:31,522 ‫Choose either of them, okay? 203 00:10:31,522 --> 00:10:34,980 ‫And if it's working, go with the Playground. 204 00:10:34,980 --> 00:10:39,660 ‫If it's not, go with this there, but, okay. 205 00:10:39,660 --> 00:10:41,880 ‫So we will stop here. 206 00:10:41,880 --> 00:10:44,520 ‫And following the next lecture, we are actually 207 00:10:44,520 --> 00:10:48,120 ‫going to start learning about what a variable is. 208 00:10:48,120 --> 00:10:51,963 ‫And we are going to start with the basics of Swift.