English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about variables, constants, literals, and examples in Swift programming.
In programming, variables are used to store data in memory and can be used throughout the entire program. However, each variable must have a unique name, known asidentifier(Identifier). You can consider a variable as a container to save information, and you can change this information later.
Technically, you can consider a variable as some books stored in a backpack, and you can also replace these books with other books later.
In Swift, we usevarkeyword to declare a variable.
var siteName: String print(siteName)
We have declared a String variable named siteNametype, which means it can only save string values. AccessSwift stringsMore information about strings can be found here.
If you try to run the above code in the Playground, it will give us a compile-time error (use of variable before initialization) because it does not store/include any value.
You can useAssignment operator(=)Assign a value in the variable.
var siteName: String siteName = "Apple.com" print(siteName)
or
You can also inline assign the value as follows:
var siteName: String = "Apple.com" print(siteName)
When the program is run, the output is:
Apple.com
Variable siteName now contains the value "Apple.com".
Since Swift is a type inference language, it can automatically infer (know) that "Apple.com" is a string and declare siteName as a String type. Therefore, you can even remove the type declaration (:String) as follows:
var siteName = "Apple.com" print(siteName)
When the program is run, the output is:
Apple.com
Since siteName is a variable, you can also change its value simply by using the assignment operator (=) without using the var keyword, for example:
var siteName = "Apple.com" // Assign a new value to siteName siteName = "w"3codebox.com" print(siteName)
When the program is run, the output is:
oldtoolbag.com
A constant is a special type of variable whose value cannot be changed. Consider a constant as a container to save information that cannot be changed later.
Technically, you can consider a constant as a backpack used to store some books, and once the books are placed in the backpack, they cannot be replaced.
In Swift, we use the let keyword to declare variables.
let siteName: String print(siteName)
We have declared a constant string named siteNametype.
If you try to run the above code, it will give us a compile-time error (use of constant before initialization) because it does not contain/retain any value.
You can use the assignment operator (=) in the same way as with a variable to assign a valueConstantin it.
let siteName: String siteName = "Apple.com" print(siteName)
or
or
let siteName:String = "Apple.com"
When the program is run, the output is:
Apple.com
Now, the constant siteName contains/holding the value "Apple.com".
Similar to variables, you can omit the type (:String) from the declaration as follows:
let siteName = "Apple.com" print(siteName)
When the program is run, the output is:
Apple.com
But unlike variables, you cannot change the value of a constant. So, you can't do this:
let siteName = "Apple.com" siteName = "w"3codebox.com" //compile time error print(siteName)
The above statement gives us an error because, as we said, once data is stored, the value of a constant cannot be changed. This is the key difference between variables and constants.
Literals are values that appear directly in the source code. They can be numbers, characters, or strings, etc. For example: "Hello, World"12,23.0, "C" is a simple example of a literal. Literals are often used to initialize variables or constants (assigning values to them).
For example:
let siteName = "Apple.com"
In the above expression, siteName is a variable, and "Apple.com" is a literal.
It represents decimal, binary, octal, or hexadecimal values. It has four types.
Binary literals
Represents a binary value.
Starting with 0b.
Octal literals
Represents an octal value.
Starting with 0o.
Hexadecimal literals
Represents a hexadecimal value.
Starting with 0x.
Decimal literals
Represents a decimal value.
Starting from zero. Everything you declare in an integer literal is of decimal type.
Example7How to use integer literals in Swift?
let binaryNumber = 0b11111111 print(binaryNumber) print(1231)
When the program is run, the output is:
255 1231
In the above program, there are two integer literals 0b11111111(binary literal) and1231(decimal literal).11111111 The decimal value is 255Therefore, the print(binaryNumber) statement outputs 255
Similarly, print(1231) Output the decimal value in the console 255
String literals are sequences of characters enclosed in double quotes, while character literals are single characters enclosed in double quotes.
Example8How to use string and character literals in Swift?
let someCharacter:Character = "C" let someString:String = "Swift is awesome"
In the above program "C" is a character literal, "Swift is awesome" is a string literal.
Used to initialize variables of data types float and double. It can have two types:
Decimal:
It can have an optional exponent represented by an uppercase or lowercase e. For decimal numbers with an exp exponent, the base is multiplied by 10 exp:
Example9How to use decimal literals in Swift?
let someFloat = 12.23 let someAnotherFloat = 3.14e2 print(someFloat) print(someAnotherFloat)
When the program is run, the output is:
12.23 314.0
In the above program12.23,3.14e2is a floating-point literal.3.14e2 Expressed in exponential form, it is equal to3.14 * 10 2
Hexadecimal:
Hexadecimal floating-point numbers must have an exponent represented by an uppercase or lowercase p. For hexadecimal numbers with an exp exponent, the base is multiplied by2 exp:
Example10How to use hexadecimal literals in Swift?
let someFloat = 0xFp10 let someAnotherFloat = 0xFp-12 print(someFloat) print(someAnotherFloat)
When the program is run, the output is:
15360.0 0.003662109375
In the above program 0xFp10,0xFp-12 is a floating-point literal. 0xFp10Expressed in exponential form, it is equivalent to15 * 210(F is represented as decimal15)。Therefore, print(someFloat) outputs15360.0。
Similarly, 0xFp-12 is equivalent to 15 * 2-12. Therefore, print(someAnotherFloat) outputs 0.00 on the screen3662109375
Swift has two boolean literals. They are true and false.。
Example11How to use boolean literals in Swift?
let result:Bool = false
In the above program, false is a boolean constant that is assigned to the constant result.
Choose a meaningful name. For example,var name than var n More meaningful
Declare variables or constants using camelCase notation. CamelCase starts with a lowercase letter. For example:
var studentName let studentAge let address
You can also define variables and constants without marking them. Not using name marking means you will not use it in the program. In many cases, you may want to create an unused variable. In this case, you can use the _ placeholder as:
var _ = "Apple.com" //The string has been initialized but not stored in a variable let _ = "Apple.com"
Even this is valid
_ = "Apple.com"
Use constants if you need to set a value only once and do not need to change it again in the program. However, if you need to change it later, use a variable.
Constant and variable names cannot contain space characters, mathematical symbols, arrows, private (or invalid) Unicode code points, or line and box diagram characters. They also cannot start with a number, although numbers may be included elsewhere in the name.
var 12 = "Apple.com" //gives a compile error: expected pattern var @hello = “Hello” //gives a compile error: expected pattern