Swift version: 3.0 | View all code snippets in this post on Github

Console Output

Use the print() function to print to the console:

print("Hello there! Welcome to the world of Pokémon!")


Variables are declared using the keyword var, followed by the variable name and optionally its type. A value can be assigned to the variable using the = operand:

var playerName: String = "Red"

Variables can be re-assigned:

playerName = "Blue"

…but cannot be nil, unless they are optional1.

playerName = nil
// -> Error


Constants are declared similarly to variables, except they use the keyword let instead:

let gameTitle: String = "Pokémon Red"

…and cannot be re-assigned:

gameTitle = "Pokémon Blue"
// -> Error

Like variables, constants cannot be nil1.

Type Inference

Type inference refers to the compiler’s ability to implicitly determine the type of a variable without having to specify it in the declaration:

var rivalName = "Blue"

String Interpolation

Values can be included in strings using a \ and the desired value between parentheses:

var totalHP = 15
var damage = 10
var currentHP = "Current HP: \(totalHP - damage)"
// -> "Current HP: 5"
  1. Variables and constants that are explicitly declared as optional can be nil. More on that later. 2