Variables and Constants in Swift

One of the most important building blocks in every programming language are variables and constants. Variables and Constants aloow us to store, manipulate and use information we need so we can actually use the application.

article

One of the most important building blocks in every programming language are variables and constants. Variables and Constants aloow us to store, manipulate and use information we need so we can actually use the application.


What are variables?


Variables in Swift can be perceived as little containers which store a value. Variables and Constants are objects which are stored in the system memory or RAM inside your Mac or iPhone. The whole idea about this, is that we can use or reuse the stored values of those variables inside our application. And because these values are stored inside the RAM, retrieving them goes almost instantly.



When we declare a variable we start using the var keyword. With var we tell the compiler that we are about to declare a object which we want to store in memory. After var we give our variable a name and after that we use the = key to assign a value. In this whole article we will declare different variables for our really cool space shooter game. This way we use a real world example of how we use variables in Swift. This makes coding more fun for everyone ;)


    
        var shipName = "Enterprise Z"
        var HP = 100 
    

In the above code example, we created 2 different variables. the shipName with a value of String and a variable HP with a Integer value. When we run our code, the variables will automatically be stored in memory and from there we can retrieve it by calling the variable names like in the following example:


    
        shipName
        HP 
    

As you see, when we call the variables we actually get the stored value of those variables. This is especially usefull when we need to adept the value of our variables like we do in the next example:


    
        shipName = "Adromeda"
        HP = 250
    

The coolest thing about variables is that we are allowed to change the intitial stored value of the variable itself. How this works is that variables adopt the latest declared value inside our code. So in our example it adopts the value of the latest entry, in our case "Adromeda" and 250. This makes variables especially usefull when we have a lot of changing values in our code.


Constants in Swift


Another way of storing values is the use of constants. Constants are almost the same as variables are with one big difference. Constants are not allowed to change value or type. Constants are mostly used when we use certain values of which we want to make sure that they don't change. Constants can be really handy to make absolutely sure that you can't change a certain value later in your code. When we declare a constant we use let instead of var:


    
        let shipClass = "Warship"
        let captain = "Picard"
    

And now the magic of constants happens. When we call the constant and try to assign a different value, the compiler will give an error telling us we are not allowed to change the value of a constant


    
        class = "Fighter"
        captain = "Kirk"
    

As you can see in your playground. The compiler returns a error complaining that you can't change the value of a constant.


Value types in Swift


When working with variables or constants we can assign values to our variables and constants. But without adding a value type, we can actually assign any value to our variables instead of a specific one. When it happens you have a variable which only needs to be a Integer value, we can actually secure the variable by adding a value type Int. This way we can only assign integer values to that specific variabel. In Swift we have 6 standard value types:


    
        var string: String = "I'm a string" // This is how we declare a standard value type of String
        var int: Int = 10 // This is how we declare a standard value type of Integer
        var bool: Bool = true // This is how we declare a standard value type of Boolean
        var float: Float = 1.1 // This is how we declare a standard value type of Floating point
        var double: Double = 1.1 // This is how we declare a standard value type of Double
    

In the above code example we have the most widely used value types in Swift. In most applications you will use the standard value types, but later in this guide we will go deeper into advanced value types. When you look at the above code example, you see 2 value types which are almost the same. That is the Float and the Double. The Float and the Double are both used when we work with numbers. But there is a difference between the Float and Double. It has all the do with the amount of numbers behind the . , a Float can store up to 7 decimal numbers. This means a Float can store up to 32 bits. A Double on the other hand is 64 bits and it allows us to use up to 15 decimal numbers. For normal applications you can always use Float if you need to, but if you need to be more specific with the numbers or results. But i think that in general, everyone uses Double these days. Using a Double is just easier to work with and it is just more logical.


Conclusion

In this article we learned the basics about Variables and Constants, Value types and how we can declare them. Make sure to try this yourselves and try to play around with it. You will find that the basics of Swift are actually really easy to understand and to use.

instructor

Exodai INSTRUCTOR!

Johan t'Sas

Owner and Swift developer!