(2020) Swift Tutorial for Beginners: Lesson 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my Swift tutorial for beginners if you want to learn Swift programming even if you've never coded before then you're in the right place I'll take it step by step and by the end of this video series you're going to have a solid foundation in Swift programming alright stay tuned my name is Chris and I'll be your guide and teacher on your swift journey now before we dive in I need to tell you about two things that are going to be critical for your success number one you have to keep in mind that this is a journey and I'll elaborate on this in a second number two is that you're going to have to get your hands dirty now these two lessons I've had to learn the hard way through my own failures when I first started learning Swift programming I failed and that was because I just kept learning and learning and as I was learning everything made sense in my head but when I finally got to coding I realized that I didn't retain a lot of what I've learned and I don't want that to happen to you so it's really critical that you practice and I'll help you along with that and the second thing is that as with any journey worth completing there will be ups and downs you probably will be pretty confused in the beginning but I guarantee you stick with it and you'll come out the other side a swift developer all right now I know you're itching to get started but just one last thing for your success if you ever get confused or get stuck you could simply leave a comment below and we're going to jump in and help you out or you can join our free code with Chris Facebook community where the team and I hang out as well as other iOS learners all right now that you have the right frame of mind let's take a look at what your Swift journey is going to look like in this lesson we're going to get you set up and oriented with the tools and we're going to dive into our first Swift topic which are variables in the next five or six lessons after that you'll learn more Swift you'll do more hands-on coding and with each lesson you progress through you'll be layering and building up your skills becoming a stronger and Swift e're programmer then from lesson seven to eleven you'll be learning more about how swift code is structured and organized and how it's actually used now even if you just get up to this point you'll have a pretty solid foundational understanding of how the Swift programming language works and how to use it but we're going to take it even further from that point on you're going to continue learning new Swift concepts and goodies that will enable you to do more and more with this wonderful programming language oh and I forgot to mention in between these lessons I'll sprinkle some challenges and exercises that I highly recommend you do because it's gonna help you discover your hidden knowledge gaps okay so with that said let's jump right in alright so first things first where do you write your Swift code well ideally you want to be using a swift code editor because that way you can compile your Swift code execute it and take a look at the results well Apple has released a tool called Xcode just for this purpose now using this tool you can write your Swift code build your user interfaces and build apps across all of Apple's different platforms and what's more this app is completely free the only drawback is that it's made for Mac computers and there is no PC or Windows alternative but if you're on a PC it's not the end of the road for you because there are other alternatives such as browser-based solutions you can type your Swift code directly into the browser and execute it and then take a look at the results you're not going to be able to build out this way but you're still going to be able to use it as a learning tool to learn Swift programming language I wrote an article explaining all of the different options that code with Chris calm slash Xcode for Windows now if you are on a Mac all you have to do in order to get xcode is go to the mac app store and then search for xcode here and you're gonna see this listing here now don't be fooled by the low ratings this is the tool that all of the professionals use to build apps on all of the Apple platforms it can be a little bit confusing at first when you launch it and from time to time it is buggy however it is the official Apple tool so it works and placed nicely with the rest of the Apple ecosystem including submitting apps into the app stores so what you're going to want to do is download and install Xcode and you have to also make sure that you're running the latest version of Mac OS because if you scroll all the way down here take a look at this compatibility section if this doesn't say works on this Mac you can you can actually click and they'll tell you which version of Mac OS it requires you want to go up here and check your version of Mac OS on your machine and if you have a lower version you can just search for Mac OS in the App Store right here and it's going to be a free upgrade for you the only question is does your machine support this latest version of Mac OS even if it doesn't and you can't get the latest version of Xcode I wouldn't let that stop you use one of the browser-based solutions because you're still going to be able to learn Swift and practice with even without Xcode all right so after you've installed Xcode go ahead and click on open and that's going to launch Xcode for you now you're gonna get this sort of welcome dialog and we're not going to create a brand new Xcode project instead we are going to get started with a playground and that's just a lightweight little window where we can type in our Swift code and execute it to take a look at the results that's perfect for learning Swift if you don't get this Welcome dialog you can always go up to the file menu go under new and then choose playground so if you have this welcome dialog just click on get started with the playground we're always going to end up in this place where we're going to choose what type of playground we want to create so under iOS choose blank playground and you can save it anywhere you want make sure you remember where you save it so you can access it I'm just going to save it on the desktop right here now before we dive in here let's do a quick tour and let me show you all of the different elements of the playground so obviously this big main section here is the code editor this is where you're gonna be typing in your swift code and there is some sample Swift code here we're not gonna worry about that just yet I also have some line numbers which makes it very easy for me to refer to the different lines of code if you don't have line numbers and you want to enable them just go to Xcode preferences under a text editing and you can turn those line numbers on or off now you'll also notice that your when you hover your mouse under the different lines this sort of blue play icon follows you and when you drag it down and highlights all of those line numbers and blue well when you click this run icon it basically just runs the code up to that point so if I drag it to line number 2 and I click this blue button it's just going to run that first line but if I drag it all the way to the bottom here at line 4 and I run this it's going to run all the code of that point up to line 4 and you can see here at the top there is a status bar which tells you the current status of the playground it should say ready for you and that means it's gonna be ready to accept your code and run it there are some options up here for some different views the one I want you to click right now is this debug area hide or show the debug area and this is also referred to as the console area when we're going to be testing our Swift code we're going to be outputting information into this debug or console area so I want you to make sure that you have this tray enabled and that you can see the output that's going to be shown there this right hand side here is a preview of your lines of code I wouldn't pay too much attention to this because from version to version of Xcode it may look a little bit different where I want you to focus is in this debug area down here because that should be consistent with what you're seeing in the video now there's one more thing I want to draw your attention to and it's this button down here now this will stop executing the playground so if I hit stop you can see that it enables this guy again and you can also click this button it's turned into a play button when you click on this icon it's going to execute your entire playground if you hover your mouse here and you choose you know a certain line of code to run the code up till that just gives you greater control if you want to run like a segment of your playground rather than the entire playground which is what you would click here now another thing is that if you click and hold this play button you can see that there are two options it's set to manually run which means that every time you want to run your playground you're going to have to either click on this play icon or choose a line and hit this blue play icon but if you click and hold it and change it to automatically run every time you edit the code it's just going to automatically execute your playground and update the results now sometimes this can be a little buggy and as you can see here it's running my playground sometimes this takes a long long time for it to be ready again and sometimes you know when it's constantly running your playground like this you're not going to see the updated results it doesn't work as perfectly as we would want so if you're having some trouble with you know you not seeing output on your playground and seeing that this is continually running then what you might want to do is just click stop and click and hold it and change it to manually run and when it says ready you're going to be able to click and run your code all right so now that you're a little more familiar with the playground and all of its controls and different windows now we can actually dive into Swift code so when you're coding you want to have the mindset that you are giving the computer instructions on how to process data now take for instance a stock portfolio app if you are building an app like that you'd have to write code to tell the computer where to grab the stock prices and then tell the computer how to manipulate that it maybe come up with the percentage lost or percentage gained and then you're gonna have to write code to tell the computer instructions on how to display that to the user in a table format another example take for instance a photo app kind of like Instagram you're gonna have to write code to tell the computer where to grab the images right and how to display them to user and if the user is taking a photo and trying to upload it to their account you're gonna have to make sure that you have code containing instructions on how to take that image data from the user's phone and then where to upload it to some sort of file server and make an entry in a database so when you're cool you're always writing instructions for the computer on how to process different types of data and in the next lesson we're going to talk more about different data types but in this lesson we are going to I'm going to demonstrate some simple pieces of data for you so let's take a look at the playground right now and we're going to ignore what's on the screen right right up here these couple lines and I'm just gonna show you some data so here some number data right that's 123 or we could have like a decimal number that's another piece of data is zero point seven or we're gonna have some piece of text and text this surrounded by two quotes like that and that's a piece of text that's another type of data now the data is stored in the computer's memory which is how the computer keeps track of where the data is and what it is in order for us to work with it we're going to need a way to reference that specific piece of data and that brings us to our topic for today's lesson which are variables and variables are a way for us to keep track of a certain piece of data so now let me show you how to write swift code to create a new variable in order to keep track of a piece of data in memory so in order to create a new variable you type in the keyword var or var followed by a space and then you can type in a variable name now you can name your variable anything you want but it should be descriptive for the data that that variable is going to keep track of so for example if I had a piece of data that was someone's first name I might name my variable first name and nearing the end of the lesson I'll tell you some more best practices about naming variables for now let's write first-name and that's going to be our variable name so just like that we've declared a new variable called first name now at the same time you can also give a piece of data for this variable to keep track of and we do that using the equals sign so you're going to write a space and then an equal sign and then you're going to write another space followed by the data that you want this variable to keep track so since we named this variable first name why don't we give it a piece of text containing the word Tom so let's recap what this line of code does so we've got a piece of text data Tom and we need a variable to keep track of it so we're declaring a new variable using the VAR keyword and we're calling this variable first name and then we're using the equal sign to assign that piece of text data to this first name variable now let's jump back into the playground and let's try this out so I'm going to erase this sample pieces of data that we had here and you can see this line of code actually this line of code which came with our playground is exactly what we just showed in our slides except that this time the piece of data is hello playground and the variable name is STR so here we're going to write in the line of code that I just showed you so you want to write V a our space and our variable name is first name followed by space and then the equal sign and then space followed by our piece of text data which is Tom now what we've accomplished here is that we've declared a new variable called first name and we've assigned the piece of data tom to it now whenever we want to work with that piece of data tom we can just refer to our variable first name so now let's try working with our variable what we're going to try to do is output Tom down here in the console area and there's a command called print followed by two parentheses like this and in the middle you can give that command something to output into the console area below so why don't we try typing in first name in there and what we're going to do is you can either hit the execute button here or you can hit this blue button right there so let's go ahead hit execute and you can see here that we've successfully output that piece of data tom so isn't that pretty cool your print command is saying that you want to print the data that is referenced by the variable first name now let's try working with another piece of data let's say var stuff price equals 100 hey and now why don't we try printing stock price and let's go ahead and click that line and we output a hundred down there now just as an aside if you're taking a look at the preview here and you might notice this slash end now this backslash end is just a line break or a carriage return that's what puts that output in a separate line down here in the console now what I want to show you is that you can actually reuse your variable so you don't have to declare it again and you can just write stock price you can change the stock price let's say 50 right you don't have to read eclair it because you've already created that variable or declared it so now down here if I print out stock price and I hit the run icon you can see that it's 50 well where that the 100 go well it's basically you've lost track of it because this line of code you're saying that now the variable stock price should keep track of the piece of data 50 so go ahead and give this a try if you're playing around you might notice something like you cannot assign a piece of text data to stock price let's say I'll just use Tom again you can assign Tom to the variable stock price and this is because the variable stock price is expecting number data only in the next lesson when we talk about data types I'm going to explain to you a little bit more about why this is but for now just know that when you declare a new variable as in line 5 or line 8 here using the VAR keyword and you assign a piece of data to it from that point on when you try to reassign data to that variable it can only be of the same type so numbers or 10 so stock price can only accept number data and first name can only accept text data so let's go ahead and erase this line here so now you know how to declare new variables and how to assign pieces of data to the variable now there's something else you can use to keep track of data and they're kind of like variables these are called constants now the only difference is that when you declare a constant instead of using the VAR keyword you use the let keyword so let's try to clearing a constant here instead of using var I use let followed by space and then I write my constant name so this time let's say it's the last name and then I'm going to use the equal sign and I'm going to assign a piece of data to my constant and I'm just gonna write Smith now what's the difference between a constant and a variable well as you saw up here with a variable you declare it once like in line 8 here and then later on you can reassign a different piece of data to that same variable with a constant you can't do that once you've assigned data to a constant you can't assign anything else to it in fact if you try it's going to give you an error so let me demonstrate if I did something like that Xcode would immediately tell you that you can't assign something else to the constant last name because that's how constants work so at this point it might seem that variables are more flexible because you can do more with them and constants seemed a little more restrictive and that's true but there are instances where you would want to use constants instead of variables in fact if you want to keep track of a piece of data and you don't intend to change that piece of data then using a constant would be preferable I wouldn't worry about it too much for now as you get farther into your Swift journey you're going to understand when to use variables versus constants and furthermore when you're coding an app and Xcode has detected that you haven't changed the data in your variable it would suggest for you to change that variable into a constant and then at that point it's just a matter of changing the VAR keyword to the let keyword but for now I just want you to know that there are something called constants which work like variables all right and the last thing I want to talk to you about is best practices for naming variables and constants now I mentioned before that you do want to be descriptive with your variable and constant names so that you know what kind of data they hold but there's a balance that because you don't want your variable and constant names to be too long because then it can also be really hard to read and you don't want them to be so short like STR or even maybe just s because then you don't know what kind of data it holds right so I would say one to four words is ideal and furthermore if you had something like three words like very first name or something like that it can still be hard to read so a best practice is to use camel casing and camel casing is just where you have the first word having a lowercase letter and then every subsequent word starting with a capital letter and you can see now that it is a lot more easy to read versus all lower caps okay so before moving on to the next lesson let's do a quick recap you learn that variables are used to keep track of information you learned how to declare a new variable you learn how to assign data to a variable you learned about naming variables and camel casing finally you learned about constants and how they're like variables except that you can't reassign data to them after the first assignment now I know that's a lot to absorb but that's why I have a practice video for you where we're going to go through a couple of exercises for you to get hands-on and dirty with I also have a set of recap notes that you can download now the links to both of these resources are down below in the description below the video now if you like this video please hit thumbs up and please subscribe by hitting the red subscribe button below alright great job with lesson one I'll see you in lesson two just click on that thumbnail right over there
Info
Channel: CodeWithChris
Views: 181,094
Rating: undefined out of 5
Keywords: Swift Tutorial, Swift Programming, Learn Swift, Swift 5, Swift 4, Swift programming language, swift tutorial for beginners, swift programming for beginners, swift beginner tutorial, swift language, iOS development, iOS tutorial, Variables, Constants
Id: ySa58y1SRy0
Channel Id: undefined
Length: 21min 53sec (1313 seconds)
Published: Tue Apr 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.