Swift Variables & Data Types
In popular media, code is often depicted almost like magical text. Often in films and television shows, you’ll see “hackers” reviewing screens of strange symbols. These narratives select the obtuse code examples because they look impressive. If you do a search for “source code in movies”, you’ll find several websites that actually explain the on-screen code. The explanations are often completely different from the plot itself.
Code is another form of shorthand writing. It’s mean to be compact and terse. Most importantly, code is designed to be human readable. Once you finish your code, it is converted into machine-readable code. This process is known as compiling.
When you write code, you are writing code to be read by yourself and other people. For this reason, you should favor simplicity over complexity. Good clean code makes your app easy to understand and update. Thankfully, the Swift programming language is written to favor this point of view.
Swift variables
Variables are a key component in any programming language. Variables hold data. Chance are, you’re quite familiar with variables. Take the following example:
x = 1
y = 2
z = x + y
What is the value of z?
The x was set 1 and y was set to 2. When you add x to y, you are adding 1 to 2. Thus, the z variable equals 3. Variable names are simply placeholders for values.
In Swift, you must designate a variable using the var keyword.
var z = 3
This is a word reserved by Swift otherwise known as a keyword. This means, you can’t create a variable called var. If you try to use it, you’ll get what is known as a compile error. A compile error means your code cannot run until you fix the error.
The var keyword is only used to define the variable. Once you define it, you can use the variable name in your code. If you try to redefine it with the var keyword, you’ll get an error.
There are some variables should never change. These are known as constants. In Swift, you define them with the let keyword.
let daysInYear = 365
A few things are happening here:
- The
letkeyword informs you that the variable will never change. Changing the variable later in your code will produce a compile error. This is a good thing. It prevents an avoidable ‘bug’ (i.e., error) from reaching the public. - The variable name is composed of several words. Swift requires variables names to be a single word so developers lowercase the first letter and uppercase each subsequent word in the name. This is known as ‘Camel Case’. It may seem weird as first, but you get used to it over time.
- This single line of code is known as statement. Many languages require statements to end with semi-colon. Swift does not.
Variable Types
When you create a variable in Swift, you are also assigning it to a data type. This data type determines the types of values that a variable can hold. For example, when you create a variable that stores an integer known as an Int, you can only assign integers to that variable. Should you create a variable that stores just a true or false value, otherwise known as a Bool, you can only store true or false value.
In some languages like JavaScript, you can change a variable’s type, but that invites uncertainty and errors into your code. Imagine trying to divide some text by two. Or, imagine multiplying a false value by the value PI. In those cases, Swift will exit the program otherwise known as a crash.
When you define a variable, Swift infers the type. You can also set the type when you define the variable.
var z: Int = 3
As you can see, this statement is a bit complex. After the variable name, you place a colon followed by the type name. This type is an integer. Types always start with an uppercase letter. As a good rule of thumb, you should define everything with inferred typing and only declare a variable type when necessary.