In this episode of our beginner’s series on Swift, we will dive into programming with the most fundamental part: assignment of values to identifiers. We’ll use the new Xcode playground feature to explore var
and let
. We’ll learn how to add numbers and a few tips about common syntax errors and the Xcode’s autocomplete feature
Edited Transcript
Welcome back to episode 3 of KinderSwift. In this lesson we’ll begin using the playground to learn to code.
Open Xcode. If you have any other projects open, close them. and if necessary restart Xcode.
If you don’t see the welcome page hit Shift-command-1 on the keyboard.
When you have a welcome page, Click Get started with a playground.
A new window asks you about the playground. Name the playground MyKinderswiftPlayground, and make sure the drop down says iOS.
Click Next and save it in the same place we saved the project from last time, which should be the default setting.Click Create, and we are in the playground.
The playground is an interactive environment that lets you see what is happening immediately. In this early part of your coding experience, it will be a lot easier and faster to use the playground than coding an app.
Delete the line right under import UIKit
. Leave the UIKit
line there. That is what makes our code work.
The first thing I’d like to teach you is this
//this is a comment
That is, as it says, a comment. Swift ignores it, but it is a great way for us to put notes to ourselves and others about what we are doing in our code. Now type this:
let myNumber = 4
This assigns the integer 4 to the identifier constant myNumber
.
Now what does that mean? Let’s break this down piece by piece.
We have the keyword let
. Keywords are words in a computer language which the system knows is a command. let is a command to make a constant, which is a value that will always stay the same.
After the let
we have a space. Spaces in programming languages are as important as in written languages because they tell us we are going to the next word.
We then have the identifier myNumber
, which is a way of naming a value. In Swift we write identifiers usually like this where we squish words together without spaces and capitalize where we are supposed to have a space.
Constants and variables like this will start with a lower case letter, to distinguish it from the types and classes we’ll learn in a few sections.
The equals sign acts as the assignment operator, another keyword that says take the value following this and make it the value of the identifier before it.
What that means is myNumber
is 4, and I can’t change it, because it is a constant. if you look to the right column in the playground it shows the value of myNumber
.
Lets add another constant type this:
//same thinglet myOtherNumber = 2
Now we made another constant with the value 2. Again to the right is the value of the number.
Besides let
there is also var
, which makes a variable. A variable is an identifier which can change values. So let’s type:
//variable var myResult = 0
The result is 0. Once you have assigned an identifier with var or let, you use the Identifier without them.
If we wanted to add two numbers together we could do this:
myResult = 4 + 4
The result shows up as 8. Every time you assign a value to myResult
it changes to that value. You can also use identifiers here. for example:
myResult = myOtherNumber + 4
The result shows up as 6. When you assign some value to myResult it change to that number. You can also use identifiers here. For example, type this:
myResult = 4 + myOtherNumber
You’ll notice the little drop-down box that appears as you type. This tells you identifiers and keywords that Xcode knows. These are auto-completion shortcuts to typing
You can use your arrow keys then the tab keys to select the value. If there is only one value you just hit tab. You can also use your mouse to click on them. You can also keep typing. The choices will refine themselves. Press tab at any time.
You use variables just like constants. You can even use the variable you are assigning and assign it back to itself. For example, type this:
myResult = myResult + myNumber
This Shows us a result of 10. myResult
was 6, and we added 4 more to it, then saved that value back into myResult
.
That is how we use a variable. We assign it either literal values like number or we assign an expression to it like adding two numbers together. You cannot do this to a constant. try this:
myNumber = 4+4
You will get a little red error circle. The red circle means there is a mistake in the syntax of this line of the program. Click on the error and you will see the error message.
Xcode is telling you that anything assigned by let cannot be changed for the life of the application. You are stuck with it.
Change it to the variable myResult
and the error goes away.
You will get an red error for misspelling an identifier. Make myResult
myresult
and see what happens. Swift like many computer languages is case sensitive. Be sure you name things correctly.
That’s it for this lesson, Next we’ll go on to doing a little more math.
The Whole Code
//This is a comment let myNumber = 4 //Same Thing let myOtherNumber = 2 //variable var myResult = 0 myResult = 4 + 4 myResult = myOtherNumber + 4 myResult = myNumber + myOtherNumber myResult = myResult + myNumber myResult = 4 + 4
Leave a Reply