Introduction
In this article, we will be learning about the Literals in swift. Just like every other language (C/C++), swift also has a concept of literal. Literal is a direct constant value of a swift variable. It can be of type number, string, character, etc.
Syntax:
let articleName = “Literals in Swift”
Explanation: Here, "articleName" is the variable name and "Literals in Swift" is the constant value of the typed string assigned to the variable. Now Let's discuss the types of literal.
Types of Literals
For every type of variable, there is a literal. In this section, we will discuss all kinds of literal.
Integer Literals
Integer literals are also of many types here, we are going to discuss all of them.
- Binary: It is used to represent binary numbers, that is, numbers in 0 and 1. It starts with 0b.
- Octal Literals: It is used to represent octal values that are digits from 0 to 7. It starts with 0o.
- Hexadecimal: It is used to represent hexadecimal value. That is, digits are from 0 to 15. It starts with 0x.
- Decimal: It is used to represent decimal numbers. Anything that we write in integer form is the integer literals.
Example: Below example shows all kinds of integer literals discussed above.
Code:
//Binary Literals
let binaryNumber = 0b11111111
print(binaryNumber)
//Octal Literals
let octalNumber = 0o12376
print(octalNumber)
//Hexadecimal Literals
let hexadecimalNumber = 0xFp10
print(hexadecimalNumber)
//Decimal
let decimalNumber = 12345
print(decimalNumber)
Output:
255
5374
15360.0
12345
Float Point Literals
A floating-point literal comprises an integer portion, a decimal point, a fractional part, and an exponent part. Floating-point literals can be expressed in decimal or hexadecimal format.
A sequence of decimal digits is followed by a decimal fraction, a decimal exponent, or both in decimal floating-point literals.
A 0x prefix precedes an optional hexadecimal fraction, which a hexadecimal exponent follows in hexadecimal floating-point literals.
Example: In this example, we discuss the floating-point literals.
Code:
//Decimal double Literals.
let decimal = 30.375
print(decimal)
//Exponent double literals.
let exponent = 3.0375e1
print(exponent)
//Hexadecimal double literals
let hexadecimal = 0xF.3p1
print(hexadecimal)
Output:
30.375
30.375
30.375
String and Character Literals
A sequence of characters is called a string enclosed in double-quotes, while a character literal is a single character enclosed in double quotations.
Example: In this example, we will show the use of the above literal.
Code:
// Character Literal
let ch:Character = "C"
print(ch)
// String Literal
let str:String = "This is coding ninjas's Coding Ninjas Studio."
print(str)
Output:
C
This is coding ninjas's Coding Ninjas Studio.
Boolean Literals
In Swift, the Bool variable represents Boolean values. Create Bool instances by assigning the result of a Boolean method or operation to a variable or constant or using one of the Boolean literals, true or false.
Example: Showing boolean literals.
Code:
let var1:Bool = false
print(var1)
let var2:Bool = true
print(var2)
Output:
false
true
Type Alias
A type alias allows you to give an existing data type a new name in your program. Following the declaration of a type alias, the aliased name can be used in place of the existing type throughout the program. The use of type aliases does not result in the creation of new types. They merely give an existing type a new name.
Example: Showing the use of type alias.
Code:
// Creating typealias.
typealias myOwn = String
//Creating variable of our type alias.
var series: myOwn = "Game of Thrones"
print(series)
Output:
Game of Thrones