Swift Extensions

Swift extensions let you add new functionality — methods, computed properties, initializers, and protocol conformances — to existing types, even ones you don't own.

What Extensions Do

An extension adds capability to an existing type — including types from Apple's frameworks or third-party libraries — without subclassing or modifying the original source. This keeps related behavior organized and lets you extend types you can't otherwise touch, like Int or String.

Extending Int

extension Int {
    var isEven: Bool {
        return self % 2 == 0
    }

    var squared: Int {
        return self * self
    }

    func repeated(_ action: () -> Void) {
        for _ in 0..<self {
            action()
        }
    }
}

print(4.isEven)   // true
print(5.squared)  // 25
3.repeated { print("Hi") } // prints Hi three times

Extending With Computed Properties

Extensions frequently add computed properties that express a value in a different form, such as unit conversions. Because they're computed rather than stored, they don't change the type's memory layout.

Extending Double

extension Double {
    var km: Double { return self * 1000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }

    func rounded(toPlaces places: Int) -> Double {
        let multiplier = pow(10.0, Double(places))
        return (self * multiplier).rounded() / multiplier
    }
}

let distance = 3.5.km
print(distance) // 3500.0

let value = 3.14159.rounded(toPlaces: 2)
print(value) // 3.14

Adding Initializers and Protocol Conformance

Extensions can add convenience initializers and make an existing type conform to a new protocol. This is the standard way to satisfy a protocol requirement without touching the type's original declaration.

Conforming to a Protocol

protocol Summarizable {
    func summary() -> String
}

struct Article {
    let title: String
    let wordCount: Int
}

extension Article: Summarizable {
    func summary() -> String {
        return "\(title) (\(wordCount) words)"
    }
}

let post = Article(title: "Swift Extensions", wordCount: 850)
print(post.summary()) // Swift Extensions (850 words)

What Extensions Cannot Do

  • Cannot add stored properties — only computed ones
  • Cannot override existing methods or properties
  • Cannot add designated initializers to classes (only convenience initializers)
  • Cannot add deinitializers
FeatureSupported in Extensions
Computed propertiesYes
Stored propertiesNo
Instance methodsYes
Static / type methodsYes
Initializers (structs)Yes
Designated initializers (classes)No
Protocol conformanceYes
Nested typesYes
Note: Use extensions to organize a large type by protocol conformance — for example extension MyView: UITableViewDelegate { ... } — instead of one massive type body.
Note: Because extensions can be added from any file in a module, they're a common way to add convenience APIs to types you don't own without subclassing them.

Exercise: Swift Extensions

What can a Swift extension add to an existing type?