Swift Interview Question

Published Apr 11, 2018
Swift  Interview Question

Question :What is Guard keyword in swift ?
Answer :
Guard Keyword : Guard is basically a safe way to create a constant that is non optional and if it is nil , if it does return nil, we are just going to go ahead and return out of that function so that we don’t have a crash.

Question : What are benefits of Guard ?
Answer : There are two big benefits to guard. One is avoiding the pyramid of doom, as others have mentioned — lots of annoying if let statements nested inside each other moving further and further to the right. The other benefit is provide an early exit out of the function using break or using return

Question : What is the question mark ? in Swift?

Answer : The question mark ? is used during the declaration of a property, as it tells the compiler that this property is optional. The property may hold a value or not, in the latter case it’s possible to avoid runtime errors when accessing that property by using ?. This is useful in optional chaining

var optionalName : String? = “John”

if optionalName != nil {

print(“Your name is (optionalName!)”)


Question : What is the use of double question marks?

Answer :
To provide a default value for a variable.
let missingName : String? = nil
let realName : String? = “John Doe”
let existentName : String = missingName ?? realName

Question : What is the use of exclamation mark !

Answer :
Highly related to the previous keywords, the ! is used to tell the compiler that I know definitely, this variable/constant contains a value and please use it (i.e. please unwrap the optional). From question 1, the block that executes the ifcondition is true and calls a forced unwrapping of the optional’s value. There is a method for avoiding forced unwrapping which we will cover below.

Question : What is the difference between let and var in Swift?

Answer : The let keyword is used to declare constants while var is used for declaring variables.

let someConstant = 10
var someVariable : String

Here, we used the : string to explicitly declare that someVariable will hold a string. In practice, it’s rarely necessary — especially if the variable is given an initial value — as Swift will infer the type for you. It is a compile-time error trying to use a variable declared as a constant through let and later modifying that variable.

Question : What is the difference between functions and methods in Swift?

Functions are globally scoped while methods belong to a certain type.Question : Difference between function and method in swift ?
Answer : when a function is defined with in the curly braces of a class it is no longer called a function but instead it is called a method.
Methods is always associated with the object of the class.
Function are not associated with the object of the class. To called a function there is no need to create object of any class.

print (arc4random(3)) is a function not a method

Question : What is “if let” statement in swift 4 ?

Answer :
if let userSetDestibnation = Destination {
print(“driving towards “+ userSetDestination)
if let is basically the syntax of optional binding in swift 4 , if we examin the above statement little closer

using the let keyword we create the new constant and the value will be set to the value of the destination optional variable and the line with in the curly braces of the if let statement will be execute only if the destination variable does not nil.
Benefit to use the if let statement or Optional binding is there is no need to force unwrapping and code is safer , will not lead to any bug or crash.

Question : What is the Swift main advantage ?

Answer : To mention some of the main advantages of Swift:

Optional Types, which make applications crash-resistant
Built-in error handling
Much faster compared to other languages
Type-safe language
Supports pattern matching

Question : What are tuples in swift language?

Answer : Tuples are used to return multiple values from a function call e.g
let person = (name : “jain”, age: “10”)
person.name — → jain
person.age — → 10

Question : What is dynamic dispatch in iOS?

Answer : Dynamic dispatch is the process of selection polymorphic operation (method or function )at run time.
Dynamic dispatch is used to decide which polymorphic method will call at run time.

Question : What is KVO and KVC in iOS ?

Answer :

KVO stands for Key value observer that allow a controller or class to observe the changes to a property. In KVO, an object can ask to be notified of any changes to a property , whenever a property changes its value, then the observer is automatically notified.

KVC stands for key value coding by which an objects property can be accessed using strings at run time rather than having to statically know the property at development time.

Question : What is the purpose of using IBAction and IBOutlets in iOS ?
Answer : IBAction and IBOutlets are macros defined to denote methods and variables in interface builder.
IBAction is associated with methods and IBOutlets are associated with variables/properties.
IBAction and IBOutlets signify that variables and methods can be used in interface builder to link the UI element.

Question : What is Grand Central Dispatch (GCD) Objective -C ?

Answer : GCD is a library that provide low level object based API ti run task concurrently while managing the threads

Types Of GCD :

Dispatch_Queue : Dispatch queue is responsible to execute the task in first in first out order (FIFO).
Serial Dispatch Queue : This queue executes one task at a time in serial order.
Concurrent Dispatch Queue : This Queue executes as many task as it can execute without waiting for the started task to finish.
Main Dispatch Queue : Globally available serial queue that executes task on the applications main thread.

Question : What is size class in ios ?

Answer : Size Class is a new technology iOS 8 onwards to allow user tio customize application UI for a given device based on its orientation and screen size.
Before iOS 8 developer had to use multiple checks to handle the UI Elements for the different screen size.

Question : What is the lifecycle of the viewController?
Answer :

i). LoadView : It is only called when the view controller is created programmatically. This makes it a good place to create your views in code.
ii). ViewDidLoad
iii). ViewWillAppear
iv). ViewDidAppear
v). ViewWillDisappear
vi). ViewDidDisappear
vii). ViewWillLayoutSubviews : If you are not using constraints or Auto Layout you probably want to update the subviews here
viii). ViewDidLayoutSubviews : This event notifies the view controller that the subviews have been setup. It is a good place to make any changes to the subviews after they have been set.

Question : What is the application lifecycle of iOS ?
Answer :

  1. didFinishLaunchingWithOptions : Very first method of the ios application , it is invoked when the application launches.
  2. applicationWillEnterForeground : this method will invoked when the application enter in foreground state from the background or killed state.
    3 . applicationDidBecomeActive : This method is call after the application enter in foreground to finish up the transition to foreground.
    4 . applicationWillResignActive : This method will called when the application is about to inactive e.g when the user receives a phone call and press the home the home button.
    5 . applicationWillTerminate : this method invoked when the user killed the application from the memory.

Question : What are the differences in table and collection in iOS ?

1 . table is consists of single column layout and have top to down scrolling.

  1. collection is consists of grid layout/multiple column layout and also have vertical and horizontal scrolling.
  2. Collection view uses customizable layout while table view uses single column layout.

Question : What is the difference ANY and ANYOBJECT ?
**Answer : According to Apple’s Swift documentation:

Any can represent an instance of any type at all, including function types and optional types.
AnyObject can represent an instance of any class type.
Question : What’s the difference between a xib and a storyboard?

Answer : Both are used in Xcode to layout screens (view controllers). A xib defines a single View or View Controller screen, while a storyboard shows many view controllers and also shows the relationship between them.

Question : What’s the difference optional between nil and .None?

Answer : There is no difference. Optional.None (.None for short) is the correct way of initializing an optional variable lacking a value, whereas nil is just syntactic sugar for .None.

Discover and read more posts from Shubham JAin
get started