Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Literals

In Swift, a literal is a direct value embedded in your source code. It represents a fixed value in your code that you've literally provided. Swift provides various types of literals:

1. Integer Literals:

You can express integer literals in decimal (base 10), binary (base 2), octal (base 8), or hexadecimal (base 16) form.

  • Decimal: 42
  • Binary: 0b101010
  • Octal: 0o52
  • Hexadecimal: 0x2A

2. Floating-Point Literals:

  • Decimal without an exponent: 3.14
  • Decimal with an exponent: 3.14e2 (which means 3.14×102)
  • Hexadecimal without an exponent: 0xC.3p0
  • Hexadecimal with an exponent: 0xC.3p2 (which means 12.1875×22)

3. String Literals:

  • Regular string literals: "Hello, World!"

  • Multiline string literals:

    """
    This is a multiline
    string literal.
    """
    

4. Character Literals:

In Swift, a character literal is a single character enclosed in double quotes, like so: "A".

5. Boolean Literals:

  • true
  • false

6. Array and Dictionary Literals:

  • Array: [1, 2, 3, 4]
  • Dictionary: ["key1": "value1", "key2": "value2"]

7. Nil Literal:

In Swift, nil is a special literal that represents the absence of a value or the absence of a valid object.

var stringVar: String? = nil

8. Interpolation in String Literals:

Swift allows you to insert expressions directly into string literals using \(...). This is useful for creating strings that include values from variables, constants, or expressions.

let name = "John"
let greeting = "Hello, \(name)!"

9. Raw String Literals:

Starting from Swift 5, you can use raw string literals that allow you to include backslashes and double-quote characters without needing to escape them:

let rawString = #"Line 1\nLine 2"#
let interpolatedRawString = #"Line \(1 + 1)"#

In raw string literals, everything between #" and '# is part of the string, including backslashes and double quotes.

Swift's strong type inference can often determine the type you intend to use based on the literal and the context in which you use it. However, you can always specify the type if needed.

1. Types of literals in Swift programming:

Literals in Swift represent fixed values in your source code. There are several types of literals in Swift, including string literals, numeric literals, boolean literals, array literals, dictionary literals, character literals, and custom literals.

2. String literals in Swift:

String literals are sequences of characters enclosed in double quotes. Here's an example:

let greeting = "Hello, World!"

3. Numeric literals in Swift:

Numeric literals represent numeric values and can be integers, decimals, or in scientific notation. Examples:

let integerNumber = 42
let decimalNumber = 3.14
let scientificNotation = 1.25e2  // 1.25 * 10^2

4. Boolean literals in Swift:

Boolean literals represent true or false values. Examples:

let isTrue = true
let isFalse = false

5. Array and dictionary literals in Swift:

Array and dictionary literals allow you to create collections directly in your code.

// Array literal
let numbers = [1, 2, 3, 4, 5]

// Dictionary literal
let person = ["name": "John", "age": 30, "city": "New York"]

6. Character literals in Swift:

Character literals represent a single character and are enclosed in single quotes.

let firstLetter = "A"
let myChar: Character = "X"

7. Custom literals in Swift:

Custom literals are user-defined representations of values. While Swift doesn't directly support custom literals, you can create custom initializers or methods to achieve similar results.

struct MyCustomType {
    let value: String
    
    init(myLiteral value: String) {
        self.value = value
    }
}

let customInstance: MyCustomType = "My Custom Literal"

8. Swift literal expressions:

Literal expressions combine literals with operators to create more complex expressions.

let sum = 5 + 3.14   // Combining integer and floating-point literals
let concatenation = "Hello" + ", " + "World!"   // Combining string literals

9. Working with literals in Swift code:

Literals are fundamental to expressing values in Swift code. They are often used in variable assignments, function parameters, and other contexts where a constant value is required.

let numberOfApples = 10
let piValue = 3.14159
let isSunny = true

let names = ["Alice", "Bob", "Charlie"]
let ages = ["Alice": 25, "Bob": 30, "Charlie": 22]