Step into Swift logo

What is a Constant?

Constant

Constant

A constant is a value that cannot be changed.

To declare a constant simply put the word let name of the constant:

let player: String = "Henrik Larsson"

If we wanted to change player:

player = "Chris Sutton"

We would not be able to do so as constants cannot be changed. You would be presented with the error: Cannot assign to value: 'player' is a 'let' constant.

Use constant by default and change to a variable if you want to change it. Xcode is pretty decent at warning you about this.


Related articles:


< All Posts