Skip to content

SwiftLet 03: The var Variable

by: 0xLeif

Posted on:November 30, 2021Β atΒ 04:06 AM

SwiftLet #03: Var

Var is the keyword to define a variable.

var variable: String

variable = "πŸ‘‹"

print(variable) // πŸ‘‹

variable = "😱"

print(variable) // 😱

Example 1

Some types allow for the use of different operators! So far we have only used the assignment operator (=). For this example we will try using the addition operator (+).

var variable: String

variable = "πŸ‘‹"

print(variable) // πŸ‘‹

variable = variable + "😱"

print(variable) // πŸ‘‹πŸ˜±

Example 2

var variable: [String]

variable = ["πŸ‘‹"]

print(variable) // ["πŸ‘‹"]

variable.append("πŸ€ͺ")

print(variable) // ["πŸ‘‹", "πŸ€ͺ"]