In this Swift Tutorials : Swift Basics lesson we will tell you how to create a variable in Swift and change its value.

Variables associate a name with a value of a particular type The value of a variable can be set to a different value in the future.

To create a variable in Swift you use var keyword.

In our example let’s describe a human using variables. A human can have a name, age and maybe a currentCountry variables that can be set to a different value.

Before you start please Create or open a Swift Playground .
For this lesson use a playground titled 1.Swift Tutorials by the : Swift Basics

Follow Along

A human name usually does not change so, let’s create a humanName variable:

Let’s create a variable named age and assign it to a value of 25

To declare a type of a variable use Type Annotation. In this example we create a String variable named currentCountry and give it a name of Canada. Type annotation is rarely used with Swift. Usually type inferred from value.

Variables can change their values, for example if we will decide to change the currentCountry value to “France” we will just need to type the following code

As a result you will see the following in your playground:

Discover all tutorials at Swift Tutorials Navigation

This lesson is a part of Swift Tutorials and Lessons created by the Team. Feel free to save and share this post to social if you find this lesson useful or helpful.