Swift forces you to declare right upfront whether an object is a variable or constant. For this you would use one of the two keywords - var for variable and let for constants.
Constants are the objects whose value cannot be changed after it has been assigned. Simplest way of declaring a constant is:
let siteName = “infiniteZest.com”
You would use let (i.e. the constant declarations) in situations where the value is not changed once it has been assigned. If you try to assign another value to it, the Swift compiler will complain:
siteName = “anotherSite.com”
In the above case, the compiler will helpfully suggest changing let to var, so that this reassignment will work. But the above line itself produces an error as it is changing the value of a constant.
In Swift, the value of a variable can be changed. There are multiple ways of declaring a variable and assigning a value or changing it.
Simplest way to introduce a new variable into your program is:
var address = “100 Main St”
Here a variable called address is declared and a string value “100 Main St”. Since this is a variable, its value can be changed.
Now the new address will be “200 Pine St”.
Whenever the value of a variable remains constant throughout its lifetime, you want to declare it a constant by using the keyword let. If the compiler sees that you have not changed value of a variable (using var) in an entire scope of it, it warns you to change var to let.
From a code safety perspective, you want to use constants (let) whenever possible. This way, value of an object will not be changed inadvertently. It also makes your intent clear - that you want value of certain object remain constant throughout its lifetime (since you used let as opposed to var).
In the above declaration of constants or variables, we have not explicitly stated the types of variables. Both the siteName constant and address variable are of type String. Swift has strong type inference capabilities.
The above lines can be written by explicitly specifying the type:
let siteName: String = “infiniteZest.com”
var address: String = “100 Main St”
Type of a variable (or constant) can be specified by using : (colon) followed by name of the type (String in this case). This is called Type Annotation. Because of Swift’s strong type inference capabilities, this is not necessary. And, it is not considered a Swift way of coding, because it adds extra words to the code without adding any extra clarity. So, this is not necessary as Swift can figure it out.
However, if you do not have a default value at the time of declaration, then the type must be specified. Following will result in an error:
Compiler will complain that the type annotation is missing. The following will be the way to declare the variables at one point and assign values to them at a later point.
let siteName: String
var address: String
// Assign values later
siteName = “infiniteZest.com”
address = “100 Main St”
For printing a value to the console, you will simply use the print() function.
// Prints infiniteZest.com
If you want to print, multiple variables (or constants), you can use multiple print statements or separate the variables by a comma.
// Prints siteName and address on two separate lines
// Prints siteName followed by address on the same line
So, as you can tell from above, the default terminator for the print output is a newline. Meaning, output of each print statement is ended with a new line.
However, you can also use your own custom terminator by specifying the value to the terminator: parameter.
print(siteName, terminator: “ @ “)
// Prints: infiniteZest.com @ 100 Main St
- Use let for declaring constants, var for variables
- Prefer let over var whenever you can for code safety
- Let Swift use type inference for code clarity
- Do not specify type (type annotation) when Swift can figure it out (same as above)
- print() function prints to the console