Core Swift types
Every programming language needs the ability to name a piece of information to be referenced later. This is the fundamental way that a collection of code remains readable after it is written. Swift provides a number of core types that help you represent your information in a very comprehensible way.
Constants and variables
Swift provides two types of information: a constant and a variable:
// Constant let pi = 3.14 // Variable var name = "Sarah"
All constants are defined using the let
keyword followed by a name, and all variables are defined using the var
keyword. Both the constants and variables in Swift must contain a value before they are used. This means that when you define a new constant or variable, you will most likely give it an initial value. You do so using the assignment operator (=
) followed by a value.
The only difference between the two is that a constant can never be changed, while a variable can be. In the previous example, the code defines a constant called...