DEV Community

Naveen Ragul B
Naveen Ragul B

Posted on • Edited on

Swift - Extensions

Extensions add new functionality to an existing class, structure, enumeration, or protocol type.

Extensions in Swift can:

  • Add computed instance properties and computed type properties
  • Define instance methods and type methods
  • Provide new initializers
  • Define subscripts
  • Define and use new nested types
  • Make an existing type conform to a protocol

Extensions can add new functionality to a type, but they can’t override existing functionality.

example Protocol :

extension SomeType: SomeProtocol, AnotherProtocol {
    // implementation of protocol requirements goes here
}
Enter fullscreen mode Exit fullscreen mode

example Computed Property - extension:

extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// Prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// Prints "Three feet is 0.914399970739201 meters"
Enter fullscreen mode Exit fullscreen mode
  • Extensions can add new convenience initializers to a class, but they can’t add new designated initializers or deinitializers to a class.

  • If you use an extension to add an initializer to a value type that provides default values for all of its stored properties and doesn’t define any custom initializers, you can call the default initializer and memberwise initializer for that value type from within your extension’s initializer.

  • If you use an extension to add an initializer to a structure that was declared in another module, the new initializer can’t access self until it calls an initializer from the defining module.

example Initializer - extension:

struct Size {
    var width = 0.0, height = 0.0
}
struct Point {
    var x = 0.0, y = 0.0
}
struct Rect {
    var origin = Point()
    var size = Size()
}
Enter fullscreen mode Exit fullscreen mode
extension Rect {
    init(center: Point, size: Size) {
        let originX = center.x - (size.width / 2)
        let originY = center.y - (size.height / 2)
        self.init(origin: Point(x: originX, y: originY), size: size)
    }
}
Enter fullscreen mode Exit fullscreen mode

example Method - extension:

extension Int {
    func repetitions(task: () -> Void) {
        for _ in 0..<self {
            task()
        }
    }
}

3.repetitions {
    print("Hello!")
}
Enter fullscreen mode Exit fullscreen mode

example Subscript - extension :

extension Int {
    subscript(digitIndex: Int) -> Int {
        var decimalBase = 1
        for _ in 0..<digitIndex {
            decimalBase *= 10
        }
        return (self / decimalBase) % 10
    }
}
Enter fullscreen mode Exit fullscreen mode

example Nested Type - extension :

extension Int {
    enum Kind {
        case negative, zero, positive
    }
    var kind: Kind {
        switch self {
        case 0:
            return .zero
        case let x where x > 0:
            return .positive
        default:
            return .negative
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)