Swift Interview Questions

Swift Interview Questions | Freshers & Experienced

  • Calvin
  • 02nd Dec, 2019

Swift Interview Questions

Q1. What is iOS Swift?

An alternative to Objective - C, Swift is a general-purpose, multi-paradigm, open-source programming language developed by Apple to be used in the development of apps for iOS, iPadOS, macOS, watchOS, and tvOS. Swift is easy to learn, modern, concise, and has a simple syntax that is far better than Objective - C that Apple developers used to. Released in WWDC 2014, Swift is designed to work with Apple's existing Cocoa frameworks and uses the existing Objective - C library. Swift is one of the most sought-after programming languages because of Apple's app rich ecosystem and the money it generates.

Q2. Enlist major advantages of Swift Programming Language?

Within four years of its release, Swift replaced Objective - C as the most used programming language for Apple app development because of the various advantage it has, such as.

  • Open-Source - Being an open-source language, Swift is a developer-friendly language. Developers can explore and enhance Swift.
  • Readability - Swift generally has a clean syntax which makes it easy to read and learn for new developers.
  • Speed - Programs developed in Swift generally execute faster than any other, which makes it easy for development.
  • Interoperability with Objective - C - Swift is interoperable with Objective - C, and codes, and API's can be shared between them.
  • Low error rate - Swift excludes most of the drawbacks present in Objective - C, so there is less chance of crash or bug while developing apps.

Q3. List major features of Swift?

The features of Swift are designed to work together to create a powerful programming language, yet easy to use.

Some of the features are,

  • Closures unified with function pointers
  • Tuples and multiple return values
  • Generics
  • Fast and concise iteration over a range or collection
  • Structs that support methods, extensions, and protocols
  • Functional programming patterns, e.g., map and filter
  • Powerful error handling built-in
  • Advanced control flow with do, guard, defer, and repeat keywords

Q4. List major tools required to develop an iOS application?

iOS is one of the popular operating systems in the world, and there are many tools available to write the software for iOS.

  • Xcode - It is the go-to software for iOS development. Developed by Apple, this IDE compiles code more quickly that helps you develop faster apps.
  • TestFlight - A tool that helps you to invite users to test your app. Developed by Apple, you can invite up to 10,000 testers to test your app.
  • Firebase - It is a backend framework owned by Google that gives services such as data store, authentication, app distribution, database, and many more if the framework is added into your app.

Apart from this, there are many more tools that enhance your app development process like Appcode, code runner, Git, etc.

Q5. Explain Dictionary in Swift?

Dictionary in swift is like any other dictionary that is used to store key-value pairs. Each entry in the dictionary is identified with its key like a hash table. With the key, you can add, access, filter, remove or modify the associated data of any objects.

var someDict = [KeyType: ValueType]() // syntax to create empty dictionary

The key type can be Int or String and must be the same for every key in a dictionary. The value type can be of any but must be the same throughout the dictionary.

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] //example dictionary in Swift

Q6. What are optionals in Swift?

Optionals in swift is a special type that handles the possible absence of value. Some variable may or may not get value during run time, to handle this, Optionals can be used. It holds value if it is given or holds no value.

var name: String? //example of optionals

Optionals variable is created by appending '?' to any type. In the above example, the name can either be a string or none.

The value in the Optionals variable can be accessed by unwrapping it (i.e) add '!' after the optional name.

var name:String?
print(name!) // accesing optinal variable

Q7. What is ?? operator in Swift?

?? or nil coalescing operator can be used in conjunction with Optionals variable, so it either unwraps an optional if it has a value or assigns it a default value if it is empty.

let something = a??b // example of nil coalescing

In the above example, 'something' either gets the unwrapped value of 'a' if it is not null or it is assigned with 'b'.

It can be interpreted as,

let something = a != nil ? a! : b

Q8. Explain Guard statement in Swift?

Guard in Swift is a control-flow statement like 'if', but unlike if statement, Guard transfers the scope out of the block if the condition is met or it executes the block.

Guard booleanExpression else{
//code statements 1
//code statement 2

In the above example, code statement 1 is executed only if the boolean expression becomes false or code statement 2 is executed. Also, statements inside the Guard must exit the scope. So, control statements like a break, continue, return, or throw must be present at the end of Guard

Q9. What is different between let an var in Swift?

They are both used to declare data in Swift.

let is used to declare the constant value. The values declared with let cannot be changed as it is immutable. Trying to change the value will result in an error.

let myName  = "priyag" //myName is constant and cannot be modified again.

var defines an ordinary variable. It is mutable and can be changed in the future.

var myAge = 20 // myAge value can be changed.

Q10. What are generics in Swift?

Generics is one of the powerful features present in Swift that lets you write flexible and reusable functions and types. You can write a function that accepts the values of any data type and work with it in Swift using Generics.

func exchange(a: inout T, b: inout T) {
   let temp = a
   a = b
   b = temp
var num1 = 10
var num2 = 20
var str1 = "Generics"
var str2 = "Functions"
exchange(a: &numb1, b: &numb2)
print("After Swapping Int values are: \(numb1) and \(numb2)")
exchange(a: &str1, b: &str2)
print("After Swapping String values are: \(str1) and \(str2)")

In the above example, the exchange function works on both integer and string data type values. The generic function and the parameter should be declared with type name 'T' inside angle brackets '<>' to accept any data type. By default, Array and Dictionary in Swift are both generic.

Q11. What is difference between nil and .none in Swift?

In normal usage, there is no difference. nil and . None are interchangeable. .None is just an enum representation of nil implemented by the Optional<T> enum. Optional<T>.None is a polymorphic primitive value, and nil is a constant.

If you don’t provide an initial value when you declare an optional variable or property, its value automatically defaults to nil. But, nil cannot be used with non-optional constants and variables. If a constant or variable in your code needs to be able to cope with the absence of value under certain conditions, declare it as an optional value of the appropriate type.

Q12. What are delegates?

Delegation is a design pattern that is frequently used in iOS development. Delegation enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. A delegate is an object that acts on behalf of, or in coordination with other objects.

Example of delegation in swift.

The UITextView uses the UITextViewDelegate to report about changes in a text view, such as inserted new characters, selection changes, and when text editing stops, and the CLLocationManager uses the CLLocationManagerDelegate to report location-related data to your app, such as an iPhone’s GPS coordinates.

Q13. Explain what is NSCache class?

NSCache class is a mutable dictionary that is used to cache some data that are expensive to create. As a result, the NSCache could improve your app performance. It temporarily stores transient key-value pairs that are evicted from the memory when the resource is low. It has various auto-eviction policies that ensures the cache doesn’t occupy much of system memory. You can add, delete, query data in the NSCache from different threads without locking the cache. Temporary value that is hard to calculate is usually stored here, so that it doesn’t have to be recalculated again, but the developer must be careful because the value can be evicted anytime if memory is constrained.

Q14. What is Deinitializer in Swift?

As the name suggests, it is the process of deallocation unused class instances when they are no longer needed. Generally, swift automatically deallocates to free up the memory space by using Automatic Reference Counting. Though, developers can manually deallocate the instance by declaring “deinit” keyword.

deinit { 
// perform the deinitialization 

A class can have only one deint block. The deinitializer does not take any parameters and is written without parenthesis. Deinitializer is automatically called before instance deallocation and cannot be called while the program is running.

Q15. What is the use of exclamation mark in Swift?

Swift uses the exclamation mark to signal forced unwrapping of optionals, implicitly unwrapped optionals, and error-handling with try.

Forced Unwrapping

Exclamation mark can be used to access optional values instead of optional binding. This way of accessing optional value is called Forced Unwrapping.

class Person {
var name: String?
let person = Person()
person.name = "John Doe"
print(person.name!) //forced unwrapping an optional value.

Implicitly Unwrapped Optionals

It means the value of the optionals can be directly accessed that there is no need to unwrap the optionals. It is defined with the exclamation mark instead of question mark.

class Person {
    var name: String!  //implicitly accessing an optional variable
let person = Person()
person.name = "John"

Error handling with try!.

Try with the exclamation mark is used if you are absolutely certain that throwing an exception won’t cause an error. If it causes error then your application will crash as try! doesn’t use do-catch.

Q16. Explain type aliasing in Swift?

In Swift, type aliasing allows you to give a new name for the existing data type into your program. If you declare an alias name for the existing type, you can use the alias instead of the type throughout your program. It doesn’t let you create a new data type, but instead makes your code more readable. Type alias can used for built-in types, user defined types, and complex types.

typealias name = existing type  //syntax for type aliasing

//example for type aliasing
typealias MyName = String
let name:MyName = "priyag"

Q17. How to get the length of a String in Swift?

There are different methods to get the count of string in different version of the Swift.

In Swift 1.0
Use count(stringname). //example let a = “hello!” println(count(a)) //output is 6. In Swift 2.0 & 3.0 Use stringname.characters.count //example let a = “how are” print(a.characters.count) //output is 7. In Swift 4.0 & 5.0 + Just use stringname.count, as the string can be treated as an array. //example let a = “you” print(a.count) //output is 3.

Q18. What are Selectors in Swift?

Selectors are effectively the names of methods on an object or struct, and they are used to execute some code at runtime. The earliest versions of Swift didn’t include some core selector functionality so their use declined for a while. That has since been restored in the newer versions.

Selectors are used when working with the target/action paradigm that you find in classes such as Timer and UIBarButtonItem. In the case of Timer, when an alarm fires, the Timer should tell who to notify(target) and what selector(action) should be performed.

Q19. What are different types of collections available in Swift?

There are three primary collection available in Swift. They are arrays, sets, and dictionaries for storing a group of related values. Collection is mutable if you assign a variable to it and Immutable if you assign a constant to it. Arrays can contain duplicate values.


It stores values of the same type in an ordered manner.

var IntArray = [Int]() //Initializing an empty integer array

Arrays can be accessed, iterated, modified, deleted, appended.


It stores distinct values of same type in an unordered manner.

var StringSet = Set() //Initializing empty string set

Along with the operations that you can do with array, you can perform set operations like union, membership, subtracting, and symmetricDifference between two sets.


It stores key-value pair. In Dictionary, all the data in the key and value must be of the same type.

var IntStringDictionary = [Int: String]() //Initializing empty dictionary that has key of Integer type and value of String type.

You can access, modify, delete, and iterate over the dictionary with its key.

Q20. What are String Literals in Swift?

A Sequence of characters surrounded by double quotation mark is called String Literals.

var eg = “hello! Interview Queries is awesome.” //example for string literals.

You can also create multi-line string literals by using three quotation marks.

Var eg2 = “””
Hello guys.
My name is priyag.
I like Interview Queries website
You can use special characters with string literals like \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quotation mark) and \' (single quotation mark). 

About Author :

  • Author of Swift Interview Questions

    Calvin Carlo

    Calvin is a Senior web developer, a Javascript enthusiast, and a technical writer. He is very interested in learning new technologies and writing technical blogs on It. He loves playing Cricket, watching Hollywood action movies and is big fan of Michael Douglas.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.