Swift Variables & Data Types

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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
var z = 3
let daysInYear = 365

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.

var z: Int = 3
See forum comments
Download course materials from Github
Previous: The Swift Playground App Next: Demo: Variables in Action