Swift: Constants

Constants in Swift:

In the previous tasks, we modified variables, but what about variables whose values we don’t want to change? Constants in Swift are in charge of this task.

Constants, unlike variables, cannot be reassigned during their lifetime. In Swift, when we assign a value to a constant, it becomes immutable. This means that the data it contains cannot be altered.

Constants are declared in Swift with the let keyword:

let radius = 4.14;

So, why do we use constants in the first place? Constants are primarily used as a safety measure. We make our programs safer by using constants, which ensure that we only change the values we expect to change, preventing some potential bugs.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: