This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
Optionals and Operators in Swift

Setting up Xcode to experiment with Swift

Xcode has a feature called “playground” for experimenting with Swift quickly. Here’s how you create a playground: Open Xcode -> File -> New -> Playground. After that, in the window that opens, select the “iOS tab” and select “Blank”. Now, name the playground whatever you want, and save it wherever you want. You are now ready to start coding in Swift!

Optionals

You declare a variable optional when you know that there is a possibility for it to have no value. You can declare a variable as optional only when you are declaring what type it is. And you can declare the type only when you create a variable. There is a very small difference between creating a variable that is not optional and creating one that is optional:

   var numberOfBones:Int=216 //This variable is not optional
   var numberOfBonesOptional:Int? //This variable is an optional

As you can see, the only difference is the change from Int to Int?. That change makes a variable of type Int that can either hold a value or not hold a value. Notice that because numberOfBonesOptional is an optional Int variable, we did not have to set it equal to a value. To create optional variables of other types, just add a ? to the end of the type name when you are creating a variable.

The nil keyword

If an optional variable is not assigned a value, it is in the state of having no value. But what do you do when you want an optional to store no value after storing a value. The nil keyword in Swift is used exactly for this. It takes a variable to a state of having no value. You take an optional variable and set it equal to nil and that’s it!

   numberOfBonesOptional=nil

This is not the only use for the nil keyword. It is used just like a value of a variable(like 29 for an Int). The only difference is that it can be assigned to variables of all types and has a value of “nothing”.

A new skill

To explain the next part of this lesson we are going to have to learn a new skill that we won’t fully understand. I promise that I will come back and explain it in detail when we are ready for it. The skill is displaying the value of a variable on the screen. It’s often called printing a variable on the screen. You can print using just this one line of code:

   print(name)

Replace name with the name of the variable you want to print.Remember, every part of that line of code is necessary.

Applying the skill

Let’s create an Int? variable called hello:

   var hello:Int?

We haven’t assigned any value to the variable hello. So, it’s value right now, is nil. Let’s print the value of hello and find out if we are right!

   print(hello)

In the area where what we print appears in Xcode, we get a result that displays nil. We were RIGHT! Let’s assign a value to hello and check if that value is displayed to us.

   hello=3421 //Assigned 3421 to hello
   print(hello)

The result of running that code is 3421 being displayed on Xcode. Remember the concept of optionals! They are extremely helpful in Swift and in iOS. Now, we will be switching over to a completely different topic.

Arithmetic operators

Arithmetic operators are operators like + and - that you can use to carry out mathematical operations. The vocab bar below gives all the arithmetic operators in Swift along with their functions:

Vocab Bar
+ Adds two numbers.
- Subtracts two numbers.
* Multiplies two numbers.
/ Divides two numbers and gives the quotient.
% Divides two numbers and gives the remainder.

Examples of how to use the operators mentioned above:

   var i:Int?

Addition:

   i=2+1 //Makes i=3

Subtraction:

   i=2934-2 //Makes i=2932

Multiplication:

   i=2*25 //Makes i=50

Division giving quotient:

   i=1000/2 //Makes i=500

Division giving remainder:

   i=5%3 //Makes i=2

Unary operators

Unary operators are the unary + and the unary . When you add a unary + in the beginning of a number or variable, it is like multiplying its value by plus 1. If you add a unary -, that’s like multiplying the value of the variable or the number by negative one.

   var a = +hello //Makes a=hello
   var b = -hello //Makes b=hello*-1

Compound assignment operators

   var count:Int=19

If count is occasionally incremented by 1, then that means that we need the current value of count to calculate its next value. We could store it in another variable called backup and then we could say:

   count=backup+1

when we want to update the value of count. But that takes way too much effort. So Swift has a shortcut for this. We can just say:

   count=count+1

That was easy! But programmers wanted it to be easier because it feels unnecessary to type count twice. That statement was shortened to:

   count+=1

This gives the same result as count=count+1. This is not just true for the addition operation but for other operations too.

   count*=2 //Means count=count*2
   count/=2 //Means count=count/2
   count-=2 //Means count=count-2
   count%=2 //Means count=the remainder when count is divided by 2

Written by Valliappan Valliappan

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.