Swift 101: Understanding Types, Variables, and Constants

silviaespanagil

Silvia España Gil

Posted on May 14, 2024

Swift 101: Understanding Types, Variables, and Constants

Hola Mundo!

This is the second article in a series of Swift 101 notes 📝 I did while learning the language and I decided to share them because why not?.

So if you don't know what Swift is or you would like to know more about this language, I invite you to follow my series!🙊

In this chapter I'll be sharing a little bit about Types, Variables and Constants and how they work and behave in Swift.


Swift is a strongly typed language, but what does that mean? It means that at the moment that the code compiles all variables and constants types are known and they cannot be changed after declaring it.

To know so, Swift will first check the variable or constant and check if we have declared the type. If we haven’t Swift will take the first value assigned and set it.

If you have worked with other heavily typed languages like for example Java, C#, or C++ then you must know that Swift works similarly.

However, if you have no experience in coding, or have more experience in languages such as Javascript, ✨​that is what happened to me when I learned Swift✨​, this might be new for you.


Variables and constants

Variables and constants are a sort of storage location that we create and associate with a certain name so we can use it along our code. Is a container for a value📦​.

As their name state variables are flexible in the sense that after declaring it, their value can change. However, constants can’t, their value is set and it cannot be changed.

To use these containers and their value we must first declare them. To do so we must use var or let followed by a keyword that will work as a name.



var variableName = <Initial value>
let constantName = <Initial value>


Enter fullscreen mode Exit fullscreen mode

So for example, let's think of a variable. We could have the userPhone that they can change later on. So we store the userPhone in a variable, and later, in a method we change it for the new one.

In the case of constants is the contrary, we have information that the user can't change. For example, delicate data like an ID number might be not editable because it's inherent to that user. So that kind of data will be stored in a constant.


Variable and constant naming

Naming a variable or constant can be tricky sometimes. The names can be composed of letters, numbers, and underscore. However, depending on the team conventions sometimes the underscore is not used and we use camelCase, which means that each time a word starts we put it in capital letters, it would look like this: thisIsTheNewVariable

Variables and constants names should also be self-explanatory, which means that is not a good practice to use namings such as var password and var password2. Because what does password2 mean, what does it do🤔​?

Is better to set names as var currentPassword and var newPassword. This way, as soon as you see the naming you already know what is stored.

If at a certain point, you find yourself not knowing how to name something just ask yourself, what does this storage? what does it do? And go in that direction.


Basic Swift Types

As stated before when we create either a variable or constant we should set its type or Swift will set it for us 🎯​.

For declaring a type we will create our variable or constant followed by : and the type name. If it already has a value we will follow the type with the = and the initial value but this last part is not necessary.



var variableName: <Variable Type> = <Initial value>
var anotherVariable: <Variable Type>


Enter fullscreen mode Exit fullscreen mode

There are many types in Swift, however, the most basic ones may be separated into two categories: individual data types and collection types.

Individual data types

As the name states these types tell the system that the value that we are storing is a single value. There are six main individual data types, three of them for different kinds of numbers, two of them with words, and the last one for booleans.

Let's have a look at them​ 👀

Type name Description Example
Int It reads as integers. This type is used for whole numbers var integer: Int = 2
Double Is another number-related type that represents 64-bit floating numbers (with decimals). Double range is set to 15 digits ≈ let pi: Double = 3.14159265358979323
Float Is used to represent 32-bit floating numbers. Float range is set to 7 digits ≈ let shorterPi: Float = 3.1415926
String To represent text, it may be words or sentences. The name comes from a string of characters. var greetings: String = "Welcome"
Character This is for a single character, such as a letter, digit, and even punctuation mark or symbols. var initial: Character = "W"
Bool Represents one of two possible values: true or false var isThisABool: Bool = true

As I said before, Swift is a strong typed language, so, if we don’t set the type, our variable or constant will be assigned by the system.

How does Swift automatically assign types? It checks the variable value and sets it.



// Will be typed as a "String"
var variableA = "It's me a Mario"
// Will be typed as a Bool
var isYoshiAvailable = false


Enter fullscreen mode Exit fullscreen mode

Collection data types

On the other hand, we have collection data types. Collections are types that contain multiple values. This means that one variable or constant can store many values in it.
There are many collection types in Swift but we are going to talk about them in another chapter of Swift 101.

So keep an eye on the next posts of this series🤓!


Resources

If you want a Cheat Sheet with the individual data types information, feel free to save or download this image. If you share it please do not delete or hide my name 🫶🏻​

Cheat sheet with all the information in the table

Also if you want to keep learning more about this I highly recommend checking the official documentation and courses here


Want to keep learning about Swift?

This a full series on Swift 101, next chapter will be about Basic Operators in Swift, so I hope to see you there!

If you enjoyed this, please share, like, and comment. I hope this can be useful to someone and that it will inspire more people to learn and code with Swift

💖 💪 🙅 🚩
silviaespanagil
Silvia España Gil

Posted on May 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related