Swift 5.1 Cookbook

Jan 7, 2021 7 min read
Swift 5.1 Cookbook

The best cookbook to learn Swift programming language.

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

Great! Next, complete checkout for full access to Codepur.
Welcome back! You've successfully signed in.
You've successfully subscribed to Codepur.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.