Creating a Variable

suggest change

Declare 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"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents