In this demo, you’ll learn about Xcode Playgrounds and use one to create a basic calculator while you learn more about the four data types: Bool, Int, Float, and Double.
Before getting started, make sure you’ve downloaded and installed the application Xcode from the macOS App Store.
Xcode is a large application, and the first time launching it might take a long time. Once it opens, though, it’ll show you the welcome window.
From the File menu, go to New ▸ Playground. Select Blank and press Next. Save the file anywhere you want; the Desktop is fine. Name the file Lesson1Demo1 so you can keep track of all the playground files you’ll create, then press Create.
Playgrounds allow you to write Swift code and execute it in sequence without having also to create all of the windows and buttons and files that make up a full app. It’s great to play around with code and try different things, so the playground’s perfect for learning.
Building the Calculator
The playground starts with two lines; delete them for now and start building your calculator.
Add these two lines to start:
let int1: Int = 10
var int2 = 6
The first line creates a new constant named int1 of type Int with an initial value of 10. The second line creates a new variable named int2 with a default value of 6.
Notice that the second variable doesn’t define the type.
Swift is smart enough to guess the correct data type you want for this variable because its a whole number with no decimal points. Swift will automatically give it the type Int. This is called Type Inference, and it’s one of the cool things in Swift.
At the bottom of the playground window, there’s a small play button that looks like a triangle. Press it to start executing your code, i.e., run your Playground.
Notice that the initial values you defined for your variables are written in the right column beside each line. This is the playground’s way of showing you what’s stored in each variable when it executes this line. It’s very helpful for understanding what your code is doing, and you’ll get to see more of this as you build the calculator.
Next, add the following two lines to change the values of your two properties:
int1 = 6
int2 = 5
Notice that Xcode complains about the first line, saying “Cannot assign to value: ‘int1’ is a ‘let’ constant”. As the error says, Xcode knows that you defined int1 with let, so you can’t modify its value. Disable this line by adding two forward slashes in front of it:
// int1 = 6
Anything written after two forward slashes // is called a comment. These are completely ignored by Xcode and don’t become part of your application. Comments are used to add explanations and notes for other people reading your code.
Boolean Expressions
In a playground, you can write expressions to do things like compare or add two numbers. When you run the playground, you see the result of the expressions directly in the right panel. Add these two expressions:
int1 == int2
int1 > int2
The double equal sign == is a Boolean comparison check that is true if the values on the right and left are equal. The single equal sign = that you used before is an assignment to store the value on the right to the variable on the left. I know this might be confusing, but you’ll get used to it quickly. And if you use the wrong one, Xcode lets you know.
Run the playground and notice the result on the right.
The two values stored in int1 and int2 aren’t equal, so the right pane shows false for that line. As for the second line, int1 is indeed bigger than int2, and that’s why it shows true.
Each of those two expressions can be either true or false. You can describe them as them Boolean expressions. You can store the results of expressions in variables or constants directly by defining a new variable and assigning the expression to it:
let boolNumbersAreEqual: Bool = int1 == int2
let boolNumbersAreDescending = int1 > int2
This is identical to:
let boolNumbersAreEqual: Bool = false
let boolNumbersAreDescending = true
Except that the results from the comparison change if you use different values for int1 and int2.
Integer Operations
Like how you created the Bool constants, create four Int constants storing the addition, subtraction, multiplication, and division results of int1 and int2:
let intSum = int1 + int2
let intSubtract = int1 - int2
let intMultiply = int1 * int2
let intDivide = int1 / int2
Run the playground to see the results of the operations and the values stored on the right.
Float Operations
Now, you’ll do the same calculations but with Float types. Create two new constants of Float type and be sure to mention the type directly to Xcode this time:
let float1: Float = 11.0
let float2: Float = 6.0
If you don’t mention : Float, Xcode uses the type Double instead. It’s not a problem at all, and the two types are very similar, as you’ll see shortly.
Now, do the same as earlier and create four constants for the four math operations:
let floatSum = float1 + float2
let floatSubtract = float1 - float2
let floatMultiply = float1 * float2
let floatDivide = float1 / float2
Double Operations
Finally, do the same as with Float but with Double constants. Create two constants with direct values and four to save the math operations:
let double1: Double = 11.0
let double2 = 6.0
let doubleSum = double1 + double2
let doubleSubtract = double1 - double2
let doubleMultiply = double1 * double2
let doubleDivide = double1 / double2
Run the playground.
All the addition, subtraction, and multiplication values are identical between Float and Double. The only different one is the division. floatDivide has the value 1.833333, whereas doubleDivide has the value 1.833333333333333. The number of decimal points in Double is much more than Float, giving you much greater accuracy.
Also, the maximum value a Double can store is much larger than what a Float can store. You might ask, “If Double is so superior to Float, why not get rid of Float?”
The answer comes down to storage. A Float value takes up half the amount of memory as a Double. This means that if you have thousands of numbers and you don’t need this very high precision, using the Float type saves you a lot of memory and performs faster as you copy those numbers around.
In the next part, you’ll learn about using text and the type String.