SWIFT 101: Variables and Constants

SWIFT 101: Variables and Constants

ยท

6 min read

I suck at being consistent when it comes to writing and I guess that's not a hidden fact ๐Ÿ˜Œ. But at some other things I can go any extra length just to be consistent. Some times, I wish there could be more than 24 hours in a day but again, wishes are not horses ๐Ÿ˜ƒ. Here's another attempt at being consistent at writing ๐Ÿ˜‰.

So, I am attending the Hashnode Bootcamp III and this is in partial fulfilment of the first task given by Sam Julien.

Variable and Constants

Like any other programming language, getting a solid foundation can be key to how far one can go in learning the language and doing anything tangible with it. I started my journey with the basics of the language and as easy as it may seem, this is me documenting it. Take for instance we have two safe boxes, we locked one with a key and the second one has neither a lock or a key. To put something in the locked one or remove something from it, we will have to use the key to unlock the box before we can replace the contents of the box. To replace the contents of the second box will not require any effort whatsoever because it is not locked.

That is just like the basics behind constants and variables. We make something (a string, an int or any other data type) a constant because we have no intention of changing the contents in your code. If we intend to change the contents of our constants, we have to go back to where we declared the constants in order to change the contents (that is using a key to open the lock). For variables on the other hand, we can declare and redeclare a variable anywhere in our code.

Constants

Constants in Swift are declared using the let key word. Take for instance, we can declare our age in line 3 of our code to be equals to 20 years.

let ourAge = 20

Once we have declared ourAge to be a constant and equals to 20, we cannot redeclare it anywhere in the code again with another value. If we discovered that we made a mistake and we are actually 21, we cannot make changes on another part of the code, say line 51 and say:

ourAge = 21

We will get an error message telling us that we declared our age as a constant and the only way to change its content like this is to make it a variable. We can either go back to line 3 and make it a variable or we simply change its value to 21:

let ourAge = 21

With this, our problem is solved. We are encouraged to use constants to store values if we know that we will not be changing its contents anywhere in our code. This helps to save memory and helps our code to load faster. On the other hand, if we are dealing with values that might change, what we need is a variable

Variables

Unlike constants, you can declare and redeclare or change the contents of your variables anywhere in your code and swift will be fine with it. Variables are declared with the keyword: var. Take for an instance, we declared our age to be 20 in line 5 of our code:

var ourAge = 20

Then on getting to line 100, we realised that we used a wrong age. Instead of going back to line 5 to make changes, we can simply make input the new value for our age and voila, everything is settled.

ourAge =  21

That is just the easy way to differentiate between variables and constants.

Type Safety and Type Inference

Type Safety

Swift as a language likes developers who keep promises. Once you make a promise to swift, it makes sure you keep to that promise or else, it will pop up little red boxes of errors until you fulfil your promise. Swift has been designed to be very safe and modern. The declarations of variables and constants we did above can also be done another way, that is we declare the variables or constants in one line or part of the code and give them values in another line or part of the code. Take for instance:

let ourAge
ourAge = 20

var ourName
ourName = "John Doe"

In some other languages, this is fine, but definitely not in swift. Swift has what we call type safety. It is us telling swift that the variable or constant we declared will carry a particular data type and we have to make sure that it carries only that data type. The codes we initialised above will only work if we state the types of data we want them to carry:

let ourAge: Int
ourAge = 20

var ourName: String
ourName = "John Doe"

This way, we have made a promise to swift that the ourAge is an integer and ourName is a string. We have to make sure we always feed it with these respective values. If for instance we come back to say in another part of our code that:

ourName = 3

Swift will give us an error message reminding us of the promise we made that ourName was going to be a string. It will advise us to input a string or change the data type "String" to data type "Any".

Type Inference

In the first instance, we declared our variables and constants and gave them initial values, still on the same line. Here, Swift does something we call type inference. This is swift's way of saying, "okay, you placed a string or integer in this variable. This means that this variable will only store a string or an integer". If after declaring on one line:

var ourName = "John Doe"

we come to somewhere else in the code and declare:

ourName = 4

Swift will give an error message, indicating that it is inferred from our initial declaration that this variable will carry only a string, why then are we putting an integer there. We will be advised to change it to a string or explicitly declare the type as: Any.

Summary

So, this is just constants and variables as well as a little bit of type safety and inference. There is more, but that will be for another day. We've mentioned one or two data types already, but hopefully we will find time to talk about them in depth.

Note:

It is worthy of note that where I used "Swift gives an error", I'm actually referring to the error message from XCODE, the ideal IDE for writing Swift and the error messages it gives.

Till Swift decides to wait for me, this is me running after Swift!