SoFunction
Updated on 2025-03-02

Detailed explanation of constants and variables in Swift programming

constant
Constants refer to fixed values ​​that a program cannot change during its execution. A constant can be any basic data type like an integer constant, a floating point constant, a character constant, or a string. It can also be an enum constant.

These constants are the same as regular variable processing, except that their values ​​cannot be modified after definition.

Declare constants
When using constants, you must declare them with the keyword let:

Copy the codeThe code is as follows:

let constantName = <initial value>

Here is a simple example of how to declare a constant in Swift:
Copy the codeThe code is as follows:

import Cocoa

let constA = 44
println(constA)


When we run the above program in playground, we get the following results.

Copy the codeThe code is as follows:

44


Type annotation
When a constant is declared, a type comment can be provided to specify which constant value type is to be stored. The syntax is as follows:

var constantName:<data type> = <optional initial value>
Here is a simple example of how to use comments to declare a constant in Swift. It is important to note here that it is mandatory to provide an initial value when creating a constant:

Copy the codeThe code is as follows:

import Cocoa

let constA = 42
println(constA)

let constB:Float = 3.14159

println(constB)


When we run the above program in playground, we get the following results.
42
3.1415901184082

Name constants
The name of a constant can be composed of letters, numbers and underscores. It must be underlined by letters or underscores. Uppercase and lowercase letters are different because Swift is a case-sensitive programming language.

Variables can be named using simple or Unicode characters. Here are valid examples:

Copy the codeThe code is as follows:

import Cocoa

let _const = "Hello, Swift!"
println(_const)

let variable = "Hello world"
println(variable)


When we run the above program on playground, we print a Chinese variable name: "Variable", and we get the following result.
Hello, Swift!
Hello World

Print constants
You can use the println function to print the current value of a constant or variable. You can interpolate a variable value by wrapping the name in parentheses and escaping the backslash before the left bracket: The following is a valid example:

Copy the codeThe code is as follows:

 import Cocoa

let constA = "ValueA"
let constB = 1101.00

println("Value of \(constA) is more than \(constB) millions")


When we run the above program on playground, we get the following results.
Value of ValueA is more than 1101.0 millions

variable
Variables provide us with a name that the program can manipulate storage. In Swift, each variable has a specific type, which determines the size of the variable storage; the range of values ​​stored in memory, and the operation group can be applied to the variable.

The following are the basic types of Swift variables

  • Int or UInt - This is for integers. More specifically, Int32, Int64 may be used to define 32 or 64-bit signed integers, which are variables used to define 32 or 64-bit unsigned integers as UInt32 or UInt64. For example, 42 and -23.
  • Float - This is used to represent a 32-bit floating point number, which is generally used to use smaller decimal numbers. For example: 3.14159, 0.1, and -273.158.
  • Double - This is used to represent a 64-bit floating point number, used for very large floating point values. For example: 3.14159, 0.1, and -273.158.
  • Bool - This represents a boolean value, true or false.
  • String - This is an ordered character collection. For example, "Hello, World!"
  • Character - This is a single character string. For example, "C"
  • Optional - This means that a variable that can accommodate a value or no value.

Swift also allows defining other types of variables, and we will learn about types like Optional, Array, Dictionaries, Structures, and Classes later.

The following sections describe how to declare and use various types of variables in Swift programming.

Variable declaration
Variable declaration means telling the compiler how much variable storage space to create. Before using variables, they must be declared using the var keyword, as follows:

Copy the codeThe code is as follows:

var variableName = <initial value>

Here is a simple example of how to declare a variable in Swift:
Copy the codeThe code is as follows:

import Cocoa

var varA = 42
println(varA)


When we run the above program using playground, we get the following results.
42

Type annotation
When declaring a variable, a type annotation can be provided to clarify which value of a variable can be stored. Here is the syntax:

Copy the codeThe code is as follows:

var variableName:<data type> = <optional initial value>

Here is a simple example of how Swift uses annotations to declare a variable. It should be noted here that if type annotation is not used, it becomes the type of the initial value of the mandatory variable, otherwise the type of the variable is declared using type annotation.
Copy the codeThe code is as follows:

import Cocoa

var varA = 42
println(varA)

var varB:Float

varB = 3.14159
println(varB)

When we run the above program using playground, we get the following results.

42
3.1415901184082


Named variables
A variable name can be composed of letters, numbers and underscores. It must be underlined by letters or underscores. Variables for uppercase and lowercase letters are different because Swift is a case-sensitive programming language.

Variables can be named using simple or Unicode characters. Here are valid examples:

Copy the codeThe code is as follows:

import Cocoa

var _var = "Hello, Swift!"
println(_var)

var variable name = "Hello world"
println (variable name)


When we run the above program using playground, we get the following results.
Hello, Swift!
Hello World
Print variables
Use the println function to print the current value of a constant or variable. You can insert the name in parentheses and escape the value of a variable with a backslash before the left bracket. The following is a valid example:
Copy the codeThe code is as follows:

 import Cocoa

var varA = "ValueA"
var varB = 1008.00

println("Value of \(varA) is more than \(varB) millions")


When we run the above program using playground, we get the following results.
Value of ValueA is more than 1008.0 millions