Variables in Swift
Reading time: 1 minLike many languages, Swift has its variables and constants. Swift offers a REPL like environment called Playground, which I have been using to learn more about the language.
Variables
We can assign a variable with an unknown type by doing the following.
var welcomeMessage = "Hello World"
As you can see we don't need a semi colon to close the declaration which is nice.
Constants
Next up let's declare a Pi constant
let PI = 3.14
actually on second thought, lets do it another way, a better way.
let π = 3.1415926535
yup, we can use unicode within our code without any problems, a nice touch.
This mean we can include a Japanese Kanji characters as a variable name too.
let 日本語= "Japanese Language".
And if unicode characters are acceptable then emoji are fair game too..
let 🐶🐮 = "pets"
let 😀a = "smiley"
I personally prefer let as it signifies that the variable won't change in the future and if you have the standard var, than you're saying that the variable will change, will raises further questions.
Inline multiples variables
var age = 19, name = "Ced", isAdult = true
Variable type declarations
We can introduce type declarations by declaring variables with our desired type. For example
var name : String
name = 55;
This will cause the following error...
// error: type 'String' does not conform to protocol IntegerLiteralConvertible'
That's it for variables in Swift.
Ced