Swift - Constants with type

Introduction

To declare the type of constant, use the colon operator : followed by the data type:

Demo

let diameter :Double = 8
print(diameter)

Result

The preceding statement declares diameter to be a Double constant.

You want to declare it explicitly because you are assigning an integer value to it.

If you don't do this, the compiler will assume it is an?integer constant.

Once a constant is created, you can no longer change its value:

let radius = 3.45
radius = 5.67   //---error---

Related Topic