Creating a Variable
suggest changeDeclare a new variable with var, followed by a name, type, and value:
var num: Int = 10
Variables can have their values changed:
num = 20 // num now equals 20
Unless theyโre defined with let:
let num: Int = 10 // num cannot change
Swift infers the type, so you do not always have to declare it:
let ten = 10 // ten is an Int
let pi = 3.14 // pi is a Double
let floatPi: Float = 3.14 // floatPi is a Float
Variable names are not restricted to letters and numbers. They can contain most other Unicode characters, with some restrictions:
Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number
Source developer.apple.com
var ฯ: Double = 3.14159
var ๐๐: String = "Apples"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents