Site icon DataFlair

Swift Literals

swift literals

Job-ready Online Courses: Dive into Knowledge. Learn More!

When programming, we encounter data represented in its raw form, like numbers, strings, or booleans. To express these values in our code, Swift provides literals. Literals represent the fixed value in our code. Integer, Boolean, and String literals are some of the literals supported in Swift.

The table below represents some of the standard literals.

Literal Literal Data Type  Example
Integer  Int 2023
Floating-Point Double 20.23
Character Character “D”
String String “DataFlair”
Boolean Bool true
Nil Optional nil
Array Array [4,8,20]
Dictionary Dictionary [“DataFlair”: 2023, “Swift”: 1973]

In this article, we will dive into Swift literals, examining their usage for integers, strings, booleans, and more. We’ll also learn how they simplify data representation and enhance code efficiency.

Integer Literals

Integer literals represent numbers without any fractional component. It can be positive or negative. It can be expressed in different formats like decimal, binary, octal or hexadecimal.

let integerLiteral: Int = 2023
print(integerLiteral)

Output:

2023

Binary Integer Literals

Binary Integer Literals represent binary values. It begins with 0b.

let binaryLiteral: Int = 0b11111100111
print(binaryLiteral)

Output:

2023

Octal Integer Literals

Octal Integer Literals represent octal values. It begins with 0o.

let octalLiteral: Int = 0o3747
print(octalLiteral)

Output:

2023

Hexadecimal Integer Literals

Hexadecimal Integer Literals represent hexadecimal values. It begins with 0x.

let hexadecimalLiteral: Int = 0x7E7
print(hexadecimalLiteral)

Output:

2023

Decimal Integer Literals

Decimal Integer Literals represent decimal values. Every integer literal declared is of decimal type.

let decimalLiteral: Int = 2023
print(decimalLiteral)

Output:

2023

Floating-Point Literals

Floating-point literals are used to represent numbers with fractional components. They can be expressed in different formats like decimal & hexadecimal.

let floatingLiteral: Float = 2023.07
print(floatingLiteral)

Output:

2023.07

Decimal Floating-Point Literals

Decimal Floating-Point Literals represent decimal values.

let floatingLiteral: Float = 2023.07
print(floatingLiteral)

Output:

2023.07

Hexadecimal Floating-Point Literals

Hexadecimal Floating-Point Literals represent hexadecimal values. It begins with 0x.

let floatingLiteral: Float = 0xF10
print(floatingLiteral)

Output:

3856.0

Character Literals

Character literals are individual Unicode values, which are numerical representations of unique characters. It handles single characters in Swift. It represents individual textual elements. It can be letters, digits, punctuation marks, symbols, and whitespace.

let characterLiteral: Character = "D"
print(characterLiteral)

Output:

D

String Literals

String literals represent a sequence of characters enclosed within double quotes. It can have a combination of letters, numbers or symbols.

Basic String Literal

let stringLiteral: String = "DataFlair"
print(stringLiteral)

Output:

DataFlair

Multi-Line String Literal

let multiLineStringLiteral: String = """
    DataFlair.
    Example of a multi-line string.
    """
print(multiLineStringLiteral)

Output:

DataFlair.
Example of a multi-line string.

String Interpolation

String interpolation allows the inclusion of identifier values within a string.

let name: String = "DataFlair"
let message: String = "Hi from \(name)!"

print(message)

Output:

Hi from DataFlair!

Escape Sequence Character

String literals cannot have unescaped characters like backslash (\) or double quotes (“). These special characters can be written in a string literal using escape sequences. The table below represents the escape sequence for the character, along with its description.

Escape Sequence Character Escape Sequence
Null Character \0
Back Slash \\
Backspace  \b
Form Feed \f
Newline \n
Carriage Return \r
Horizontal Tab \t
Vertical Tab \v
Single Quote \‘
Double Quote \“
Octal number \000
Hexadecimal number \xhh

Boolean Literals

Boolean literals represent boolean values, either true or false. They are commonly used for logical comparisons and control flow in our programs.

let booleanLiteral: Bool = true

print(booleanLiteral)

Output:

true

Type Alias

Type aliases are alternative names for existing data types. By using the typealias keyword, we can define custom names for complex data types. They help to improve code readability when working with lengthy data types. It makes the codebase more maintainable and clear.

let example: (String, Int)						// Original Type
typealias typeAliasExample = (name: String, year: Int)	// Type Alias
let example1 : typeAliasExample  = ("DataFlair", 2023)	// Usage of Type Alias
print(example1)

Output:

(name: “DataFlair”, year: 2023)

Type Inference

Swift has one powerful capability, which is type inference. It means the compiler can automatically deduce the data type of a variable based on the context.

let intExample = 2023 		      // infers it as an Int
let stringExample = "DataFlair" 	// infers it as a String

print(stringExample, intExample)

Output:

DataFlair 2023

Conclusion

Swift literals simplify data representation by directly expressing fixed values in code. Integers, floating-point numbers, characters, and strings are among the supported literals, enabling more expressive and efficient app development. By leveraging Swift literals, we can create concise and readable code, leading to more effective and maintainable applications.

Exit mobile version