Labs ICT
โญ Pro Login

Extensions

Adding functionality to existing types.

Extensions in Swift

Extensions let you add new functionality to existing types โ€” even types you didn't write, like those from the Swift standard library. You can add methods, computed properties, initializers, and more without modifying the original source code.

extension Int {
    func squared() -> Int {
        return self * self
    }
}

let num = 5
print(num.squared()) // 25
print(10.squared()) // 100
Try it Yourself ->

Adding Methods

The most common use of extensions is adding methods to existing types. This keeps your code organized by grouping related functionality together and makes types more useful without cluttering the original definition.

extension String {
    func reversedWords() -> String {
        return self.split(separator: " ")
            .reversed()
            .joined(separator: " ")
    }
}

let phrase = "Hello World Swift"
print(phrase.reversedWords()) // Swift World Hello
Try it Yourself ->

Computed Properties

Extensions can add computed properties to existing types. These don't take up any additional storage โ€” they're calculated on the fly. This is great for adding convenience properties that derive their value from existing data.

extension Double {
    var celsiusToFahrenheit: Double {
        return self * 9 / 5 + 32
    }
    
    var isPositive: Bool {
        return self > 0
    }
}

let temp = 25.0
print("\(temp)ยฐC = \(temp.celsiusToFahrenheit)ยฐF")
// 25.0ยฐC = 77.0ยฐF

print((-5.0).isPositive) // false
Try it Yourself ->

Extending Standard Library Types

One of the most powerful features of extensions is the ability to enhance Swift's built-in types. You can add methods to Array, String, Int, and any other standard library type to make them work better for your specific needs.

extension Array where Element: Numeric {
    func sum() -> Element {
        return reduce(0, +)
    }
}

let numbers = [1, 2, 3, 4, 5]
print(numbers.sum()) // 15

let doubles = [1.5, 2.5, 3.0]
print(doubles.sum()) // 7.0
Try it Yourself ->

Protocol Conformance Through Extensions

Extensions are a clean way to add protocol conformance to existing types. Instead of modifying the original type definition, you can add the protocol conformance in an extension, keeping your code organized and the original type clean.

extension CustomStringConvertible {
    var description: String {
        return "Custom: \(self)"
    }
}

struct Temperature {
    let celsius: Double
}

// Add CustomStringConvertible conformance via extension
extension Temperature: CustomStringConvertible {}

let temp = Temperature(celsius: 36.5)
print(temp) // Custom: Temperature(celsius: 36.5)
Try it Yourself ->

Keeping Code Clean with Extensions

Extensions help you organize code by grouping related functionality together. A common pattern is to define a type in one file and split its extensions across multiple files or sections, making large types easier to understand and maintain.

// MARK: - Core functionality
struct BankAccount {
    var balance: Double
    
    mutating func deposit(_ amount: Double) {
        balance += amount
    }
}

// MARK: - Convenience extensions
extension BankAccount {
    mutating func withdraw(_ amount: Double) -> Bool {
        guard balance >= amount else { return false }
        balance -= amount
        return true
    }
    
    var isEmpty: Bool {
        return balance == 0
    }
}

// MARK: - Display extensions
extension BankAccount: CustomStringConvertible {
    var description: String {
        return "Balance: $\(balance)"
    }
}

var account = BankAccount(balance: 100)
account.deposit(50)
print(account) // Balance: $150.0
print(account.isEmpty) // false
Try it Yourself ->

๐Ÿงช Quick Quiz

What does an extension do?