Swift - Introduction Variables


You define a variable in Swift using either the let or the var keyword.

In our playground there is already one variable defined:

var str = "Hello, playground" 

A new variable called str with the string value "Hello, playground".

We can create new variables and constant variables of different types:

var myVariable = 123 
let myConstantVariable = 123 

var keyword defines a variable.

let keyword defines a constant.

Swift is a statically typed language.

Once a variable has a type, it cannot be changed.

We've been letting the compiler work out what it should be.

This means our variables have been implicitly typed.

Related Topics