Loading
Lesson 01
Courses / Swift
Learn Swift - INTRO: Print, Constants, and Variables

Video Transcript

Hello guys, this video will be a quick start to learning the Swift programming language. I'll be using Swift 2 version 2 with Xcode 7. Now first thing you want to do is open Xcode and we're gonna create a playground. Playground is basically an interactive way of programming in Swift, an interactive environment. So let's go to Xcode, open Xcode. Once you're there, you're gonna go File, New, and you want to do File again and choose iOS, Source and a Playground. Okay, click Next, give it a name, my first playground and where you want to save your project, your playground. I mean, there it is. So basically it's an interactive environment and every time you add code, it will automatically display, execute it for you. So right here. So let's delete everything and start from scratch. Okay. So right now it's automatic mode. So it executes the playground automatically, right here, automatically run. Okay. So let's start off by learning the print statement in Swift. Very simple, very similar to other languages that I print. And under print, this is your string, what message you want to display, let's display the simple Hello World. And you don't need a semicolon to finish the statement, you can just say the statement no semicolon is needed. And that's it. And on the right hand side, you have this bar that tells you like this string right here, what the value of it is, it's a string and followed by a new line character. If you want to see the output of the your code, you click here, this icon right here, bottom left shoulder debug area. And my message is here Hello World, this statement produces this output. Please ignore these messages here. And okay. Now, if you want to print something else, let's say how are you close quotes, those parentheses. So you're going to see it here Hello World. How are you notice that the print statement here, the print function adds a new line. And you can see backslash and which means new means new line is added. So that's why it breaks a line after Hello World. Now let's learn about constants and variables. So the difference between constant and a variable is that for constants, you only assign a value to it once and it cannot change after that. While a variable you can assign any value to it. And even after assigning an initial value, you can change it afterwards. So let's just play around with constant for now. So in Swift, to declare constant, you say the lat keyword, and give it a name, for example, lat my name. And if you want to initialize something, you just initial assignment operator of the equal sign, and put a string with john. So I'm assigning the string john to the variable to the constant, I'm sorry, constant my name, because I'm using let. So basically, I did an assignment, assigning john to my name, and that can only be done once. So if I wanted to change the value of whatever my name is referring to, later on, I wouldn't be able to do it because it's not a variable, it is a constant. Okay, so see, there's john is storing in the constant, my name. And if you want to display the value of the constant, you can just use the print function. And here's what you're going to do. So within the quotes, you're going to say like my name is now if you want the variable, the value of a variable or constant to be substituted within the print statement, you have to do a backslash, open parentheses, the name of the variable or the constant, any close parentheses, and whatever is within this construct is going to be substituted with the value. Okay. So if you see right here, you may I output message, I'll put window here, I have my name is john, because it went from my name is and then it got this backslash with a constant name within parentheses, that means you're going to get whatever the constant is storing, which is john and substituted here in this whole thing. And you get my name is john. Okay. Now let's see. My name is a constant. Let's see if we try to change it. What would happen? What would happen? You see the problem here. The error here cannot assign to value. My name is a lat constant. Well, as I told you before, if you have a constant, you cannot change it after it's first assignment, you assign a value to a constant, then you cannot change it later on. That's why suggesting you're here, change lat to bar to make it mutable. That's when the variables come along, variables can be changed as many times as you want, doesn't have to be declared once. So let's now make my name a variable. That's just change lat to var. That means you're going create a variable called my name, you're going to assign john to that variable. And now the same way to print, you use the backslash, and you put the variable name within the parentheses, and that will be substituted in the print statement. It says my name is john. And now when I did the my name, I reassigned it to carry it worked. This is what's being stored in my name, and there's no errors, because now my name is a variable and it can be changed as many times as we want. I could change it again. Let's just print it to make sure. Now my name is backslash variable name. What's the parentheses? What put my name is carry. Okay, now let's change my name again. Ronald print. My name is name. What do we get? Okay. My name is Ronald. So variables can be changed, why constants can only be defined once with a variable with a value. I'm sorry. Okay. Now let's all these various this variable here my name is being assigned on a string. Okay. So it was inferred that the data type was string, because you didn't specify any type or your variable. So it implied it's a string, because john is a string. If you want to explicitly define a declare variable, you would do it like this variable, let's say my age. And you have to add a colon and followed by the data type. Let's see my age is an integer. So in Swift, that's a int. Okay, the first I uppercase. And that declares a variable my age. And you explicitly define declaring it to be an integer type. If you want to initialize it to something, you can do so. For 24. And now let's display its value. My age is max flesh. See what we get. My age is 24, which is exactly what's installed in the integer variable my age. Okay, so it's optional in this case to explicitly define the type. Because if you just remove that, Swift will know that's an integer because 24 here is an integer. So it implied it. Okay, you're dealing with the integer variable. So I removed it and that's the output here. Do it again. Same thing my age is 24. So nothing changed. Okay, now I would like to talk about a few other data types. So we learned just learn the integer. So the other basic data types are the double and the string which we already played with. So let's just do the double. So let's declare a variable and explicitly say it is a double and assign the value 4.24 to it. So variable my number explicitly say it's a double. So colon followed by the type. And let's initialize to what did I say? I forgot already. Anyways, I put 6.24. Okay. And now okay, so I have 6.24 decimal numbers starting this double variable called my number. Now let's display the bar. My number is remember backslash variable name within parentheses. And there is wait for the output right here. My number set is 6.24 indeed it worked. Okay, let's move on to string. We already worked with strings. If you want to declare a string variable, let's explicitly declare it my sentence type string. Okay. First letter is uppercase. Let's initialize it to I don't know. Hello world. Okay, now let's print the value. Let's just say whatever is in the variable. So my sentence. Okay. Hello world indeed it's there. Now for another type the Boolean type. Boolean type is just a type which can be either true or false. So let's try creating a Boolean variable. That's false for my call it my condition explicitly declare it as bool for short for Boolean. Okay. Initialize it to false. Now display the value. My condition. Yeah, we get that false indeed. Now let's say it's true. True. All right. Now just do the in again in case you forgot declare an integer variable my integer type and first letter little uppercase. Let's do it 48. And let's sprint my integer is next slash variable name. Oops, I made a typo. I'm sorry, integer. Okay, close parenthesis. Remember, don't need a semicolon and Swift. All right, my integers 48. Right here. Remember, these are explicitly defined types, you don't have to define it. It can be being for that this variable my integer is an int data type. And just put it back there. Okay. So that's pretty much it for this video. Just play around with these basic print statements and then declaring variables. And I just one more thing, you don't have to necessarily initialize the variables right after declaring them. So, for example, I could just get variable like say, my age type int. I could just say it like that. So I declare the variable my age of type int. And I can later on initialize this is something 47, and then just play its value with the print function. My ages, max slash variable name within parenthesis. And there it is. So first I declare my age as an ant variables, then I set a value assigned to 47 to my age, and then I display the value is in the print function. And my age is 37. All right. Got it. So that's it. Thank you for watching. And if you like the video, subscribe.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: