iOS Interview Questions(Swift Extension)

Bhoopendra Umrao
3 min readJun 18, 2019

Extension in swift plays very important role in development. Extension are used to confirming protocol’s, code separation, adding custom functionality in existing classes etc. Extension can be created for any class, structure, enumeration, or protocol.

It is also a very popular topic among interviewer for the questions. Here are some questions which are frequently asked in interview.

Q. What is Extension in swift ?

Ans: Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modelling). To create extension use ‘extension’ keyword.

extension SomeType {

// new functionality to add to SomeType goes here

}

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

NOTE

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

Q. Is it possible to add computed properties in existing type using extension ?

Ans: Yes, one can add computed properties using extension in existing types.

extensionDouble {

var km: Double { return self * 1_000.0 }

var m: Double { return self }

}

Q. Can init can be defined in extension ?

Ans:Extensions can add new initializers to existing types. This enables you to extend other types to accept your own custom types as initializer parameters, or to provide additional initialization options that were not included as part of the type’s original implementation.

Extensions can add new convenience initializers to a class, but they cannot add new designated initializers or deinitializers to a class. Designated initializers and deinitializers must always be provided by the original class implementation.

If you use an extension to add an initializer to a value type that provides default values for all of its stored properties and does not define any custom initializers, you can call the default initializer and memberwise initializer for that value type from within your extension’s initializer. This wouldn’t be the case if you had written the initializer as part of the value type’s original implementation

Q. Methods can be defined in extension, yes or not ?

Ans: Yes,Extensions can add new instance methods and type methods to existing types.

Q. Mutating methods can be define in extension, Yes or not ?

Ans: Yes,Instance methods added with an extension can also modify (or mutate) the instance itself. Structure and enumeration methods that modify self or its properties must mark the instance method as mutating, just like mutating methods from an original implementation.

extension Int {

mutating func square() {

self = self * self

}

}

Thank you !

If you having any query regarding this tutorial ? Reach me : umrao595@gmail.com

--

--