Swift version: 3.0 | View all code snippets in this post on Github
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
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 gameTitle: String = "Pokémon Red"
…and cannot be re-assigned:
gameTitle = "Pokémon Blue" // -> Error
Like variables, constants cannot be
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"
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"