Swift 5.1 Cookbook

Declaring Constants and Variables

// Declaring a constant using the let keyword
let score: Double = 5.0
// score = 10.0 // Error: You can't reassign a
let inferredScore = 20.0 // Inferred as a Double

// Declaring a variable using the var keyword
var age: Int
age = 22 // OK: You can reassign a var

Numeric Type Conversion

let score = 8
let scoreAsDouble = 8.0
// let sum = score + scoreAsDouble
// Error: type mismatch

// Use an opt-in approach that prevents hidden
// conversion errors and helps make type conversion
// intentions explicit
let sum = Double(score) + scoreAsDouble
// OK: Both values have the same type