Hey again, let’s dive deeper into Swift. Power up your Xcode playground and let’s get going!
Creating a Variable
var myVariable = "Hello"
Using the var keyword, you can define a variable as in JavaScript. The var keyword accepts various types (int, BOOL, float, etc). By default, the word Hello is a string (a line of text) because it is enclosed in double quotes. However, what if you didn’t want a string? Perhaps you want to set a number.
var myVariable = 3
You can also create a string (or any other type for that matter), my explicitly declaring it a string. Let’s check that out.
var myVariable: String = "Hello"
That’s the same as the first line of code. As such, you can also define a double in the same manner.
var myVariable: Double = 5.0
Easy? It’s quite simple!
Creating a Constant
Use the let keyword to define a constant. Let;s take a look at an example.
let myVariable = 2
Bam, there’s a constant. See the below code for defining a BOOL.
var myVariable = true