Variables in Swift

In every programming language, variables are used to store and retrieve values. These values are of different types (string, integer, floats, and Booleans, to name a few).

Swift variables, depending on their usage, can be broadly categorized three ways:

  • Variable
  • Constant
  • Property

Regardless of their category, the purpose of a variable in Swift is the same: as a container to store and retrieve values within the code.

Variable or Constant?

The basic difference between a variable and a constant is the ability to modify its value.

A Swift variable (keyword: var) can store a value which can be modified later in the code. Use this type if you know your value will change at some point during your program’s execution.

var today = "Tuesday"

Conversely, a Swift constant (keyword: let) is strictly unchangeable once a value is assigned to it. That is, it can be initialized only once. The stored value will always remain constant throughout the code execution.

let weekDays = 7

The let keyword tells the complier to keep the value constant. If you try to change the value later in the code:

weekDays = 30

you’ll get an error.

(We’ll cover the third type, Property, in a later tutorial.)

 

Naming Variables

Below are a few examples of Swift variables. They’re declared with the keyword var, followed by a colon and the data type. They can be initialized immediately:

var message :String = “Hello Swift”
var totalDays :Int = 4
var angle :Float = 10.5
var xPosition :Double = 210.532
var isSelected :Boolean = false
var myChar :Character = “S”

Naming a variable is your job. (In the examples above, the variable names are messagetotalDays, angle, xPosition, isSelected, and myChar.) You can name a variable just about anything you like. Just be sure to follow the customary naming conventions:

  1. Keep your variable name meaningful
  2. Start the name with a lower case letter, then use camel case if you’re combining words. That means subsequent, adjoining words should start with a capital.
    Examples: angleisSelected, myChar, totalDays.
  3. Whitespace is not allowed within a variable name
  4. Don’t start your variable name with a number. (You can use a number in the middle or at the end of the name.)
    Example: 1Day is not allowed; day1 is allowed.
  5. Avoid using Swift keywords as variable names.
    If you do, you must escape them with backticks. Example: var `class` = “Hello, Swift”
Fun fact:

In Swift, variable names can use Unicode characters. So you can use an emoji (or a mathematical symbol) as a variable name:

var ❤ = 3

This feature of supporting Unicode characters might be helpful in terms of internationalization. It can also help you keep your variable name compact and to the point.

 

Type Annotations

In the variable examples above, note the type annotation after the colon (e.g., String, Int, Float, Double, Boolean, Character). These represent Swift data types.

When you assign a value to a variable at the same time you declare it, it’s not necessary to specify a data type. That’s because Swift is a type-safe language. It infers the variable type from the value you specify when you declare it.

So the variable examples can be written as follows:

let message = “Hello Swift”
var totalDays = 4
let angle = 10.5
var xPosition = 210.532
var isSelected = false
var myChar = “S”

If you’re declaring a variable without assigning a value right away, you will need to specify a type:

var message :String

Variables of similar types can also be declared and/or initialized in one line:

var day1, day2, day3 : String
var x=2.5, y=0.0, z=5.0

 

Literals

A literal is the value assigned to a variable.

let myCar = “Ferrari”

The string value “Ferrari” is called as a string literal.

In Swift, literals can also be a Unicode character or an emoji:

let smiley = “☺”

Happy coding!