Many of the core operations in any programming language involve manipulating text, numbers, and determining true and false statements. Let's learn how to accomplish these operations in Swift by taking a look at its basic types and learning how to assign constants and variables. In doing so, we will touch on Swift's static typing and mutability system.
Getting ready
Open a new Swift Playground in Xcode. The previous recipe explains how to do this.
How to do it...
Let's run some Swift code that explores the basic types, and then we can walk through it step by step:
- Type the following into the new playground file:
let phrase: String = "The quick brown fox jumps over the lazy dog"
let numberOfFoxes: Int = 1
let numberOfAnimals: Int = 2
let averageCharactersPerWord: Float = (3+5+5+3+5+4+3+4+3) / 9
print(averageCharactersPerWord) // 3.8888888
/*
phrase = "The quick brown ? jumps over the lazy ?" // Doesn't compile...