In some languages, an integer number is regarded as a primitive data type. In Swift, it’s a composite data type called struct (a shortcut for a structure).
When you look for the Int declaration in the Swift language documentation, you’ll see this:
The struct keyword confirms what I’ve just said that the Int type is in fact a structure. You don’t have to worry about a @frozen attribute for now. It just means that the future versions of the language can’t change the declaration of this type by adding, removing, or reordering its properties.
The fact that Int is a struct allows it to have some additional features. For example, you can use the negate() method to inverse the value stored in the variable of the Int type.
In your Playground, remove the code with the score variable and keep just the first two lines. Below, write this:
When you run the code, you’ll see in the Console on the right that the initial value 40 has been changed to -40.
That’s the best part about variables, their values can change. But if you want to make sure that the value is not changed, especially by mistake, you use constant instead. Once you assign a value to a constant, it becomes immutable which means that the value stored in it can not be changed.
Constants are declared with a keyword let.
Write this code below and see what happens once you run it:
As you can see, the compiler returned the error and even offered a suggestion to change ‘let’ to ‘var’ to make the yearOfBirth mutable. In other words, to be able to change its value.
So, let’s change the let keyword to the var keyword to get rid of the error:
But there’s one more thing. As you can see, when defining the variable yearOfBirth, I didn’t specify the data type, but apparently it didn’t matter - how come? That’s what you’ll find out in the next article.