# Extensions in swift (Add functionality of an existing class in swift)

# Swift - Extensions

Functionality of an existing class, structure or enumeration type can be added with the help of extensions. Type functionality can be added with extensions but overriding the functionality is not possible with extensions.

**Swift Extension Functionalities** −

- Adding computed properties and computed type properties
- Defining instance and type methods.
- Providing new initializers.
- Defining subscripts
- Defining and using new nested types
- Making an existing type conform to a protocol

Extensions are declared with the keyword 'extension'

**Syntax**

extension SomeType { // new functionality can be added here }

Existing type can also be added with extensions to make it as a protocol standard and its syntax is similar to that of classes or structures.

extension SomeType: SomeProtocol, AnotherProtocol { // protocol requirements is described here }

## Computed Properties

Computed 'instance' and 'type' properties can also be extended with the help of extensions.

extension Int { var add: Int {return self + 100 } var sub: Int { return self - 10 } var mul: Int { return self * 10 } var div: Int { return self / 5 } } let addition = 3.add print("Addition is \(addition)") let subtraction = 120.sub print("Subtraction is \(subtraction)") let multiplication = 39.mul print("Multiplication is \(multiplication)") let division = 55.div print("Division is \(division)") let mix = 30.add + 34.sub print("Mixed Type is \(mix)")

When we run the above program using playground, we get the following result −

Addition is 103 Subtraction is 110 Multiplication is 390 Division is 11 Mixed Type is 154

## Initializers

Swift 4 provides the flexibility to add new initializers to an existing type by extensions. The user can add their own custom types to extend the types already defined and additional initialization options are also possible. Extensions supports only init(). deinit() is not supported by the extensions.

struct sum { var num1 = 100, num2 = 200 } struct diff { var no1 = 200, no2 = 100 } struct mult { var a = sum() var b = diff() } let calc = mult() print ("Inside mult block \(calc.a.num1, calc.a.num2)") print("Inside mult block \(calc.b.no1, calc.b.no2)") let memcalc = mult(a: sum(num1: 300, num2: 500),b: diff(no1: 300, no2: 100)) print("Inside mult block \(memcalc.a.num1, memcalc.a.num2)") print("Inside mult block \(memcalc.b.no1, memcalc.b.no2)") extension mult { init(x: sum, y: diff) { let X = x.num1 + x.num2 let Y = y.no1 + y.no2 } } let a = sum(num1: 100, num2: 200) print("Inside Sum Block:\( a.num1, a.num2)") let b = diff(no1: 200, no2: 100) print("Inside Diff Block: \(b.no1, b.no2)")

When we run the above program using playground, we get the following result −

Inside mult block (100, 200) Inside mult block (200, 100) Inside mult block (300, 500) Inside mult block (300, 100) Inside Sum Block:(100, 200) Inside Diff Block: (200, 100)

## Methods

New instance methods and type methods can be added further to the subclass with the help of extensions.

extension Int { func topics(summation: () -> ()) { for _ in 0..<self { summation() } } } 4.topics(summation: { print("Inside Extensions Block") }) 3.topics(summation: { print("Inside Type Casting Block") })

When we run the above program using playground, we get the following result −

Inside Extensions Block Inside Extensions Block Inside Extensions Block Inside Extensions Block Inside Type Casting Block Inside Type Casting Block Inside Type Casting Block

topics() function takes argument of type '(summation: () → ())' to indicate the function does not take any arguments and it won't return any values. To call that function multiple number of times, for block is initialized and call to the method with topic() is initialized.

## Comments (0)