O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining constants and variables

Constants and variables must be defined prior to using them. To define a constant, you use the let keyword, and to define a variable, you use the var keyword. The following code shows how to define both constants and variables:

// Constants 
let freezingTemperatureOfWaterCelsius = 0 
let speedOfLightKmSec = 300000 
 
// Variables 
var currentTemperature = 22 
var currentSpeed = 55 

We can declare multiple constants or variables in a single line by separating them with a comma. For example, we could shrink the preceding four lines of code down to two lines, as shown here:

// Constants 
let freezingTempertureOfWaterCelsius = 0, speedOfLightKmSec = 300000 
 
// Variables 
var currentTemperture = 22, currentSpeed = 55 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required