iOS Interview Questions (Swift) — Part 1

Animesh Mishra
May 3, 2018 · 4 min read
  1. What is the Swift and its advantages?

Swift is a general-purpose, multi-paradigm, compiled time programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, and Linux.

  • Readability — Clean syntax, which makes it easier to read and write.
  • Maintenance — Less Code & Less Legacy — one file .swift.
  • Safer Platform — You can compile, and fix the errors while writing the code.
  • High Speed — The incredibly quick and high-performing LLVM compiler transforms the Swift code into optimized native code to get the most out of its devices. The syntax and standard library have also been tuned to make the code perform optimally.
  • Swift Supports Dynamic Libraries
  • Open Source

2. Explain difference between class and structure.

There are mainly four difference between class and struct in swift. Classes have below additional capabilities that structures do not:

  1. Type Casting : Type casting enables you to check and interpret the type of a class instance at runtime.
  2. Reference counting allows more than one reference to a class instance. (Structs are value types and Classes are reference types.)
  3. Inheritance enables one class to inherit the characteristics of another.
  4. Deinitializers enable an instance of a class to free up any resources it has assigned.

Structures are always copied when they are passed around in your code, and do not use reference counting. Structure instances are always passed by value, and class instances are always passed by reference.

3. When to use class and when to use struct?

As a general guideline, consider creating a structure when one or more of these conditions apply:

  • The structure’s primary purpose is to encapsulate a few relatively simple data values.
  • It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
  • Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
  • The structure does not need to inherit properties or behavior from another existing type.

In all other cases, define a class, and create instances of that class to be managed and passed by reference.

4. How to pass a variable as a reference?

We can pass variable as a reference using inout parameter. inout means that modifying the local variable will also modify the passed-in parameters.

var value: String = “Apple”

func changeString(newValue:inout String) {
newValue = “Samsung”
print(newValue) // Output:Samsung
print(value) // Output:Samsung


5. What is swift module?

  • A module is a single unit of code distribution.
  • A framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.
  • Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift.

6. What are the different Access Levels in swift?

Swift provides five different access levels for entities within your code.

  1. Open access: Classes with open access can be subclassed or overridden by subclasses within the module where they’re defined and within any module that imports the module where they’re defined.
  2. Public access: Classes with public access can be subclassed or overridden by subclasses only within the module where they’re defined.
  3. Internal access: enables entities to be used within any source file from their defining module, but not in any source file outside of that module.
  4. File-private access: restricts the use of an entity to its own defining source file.
  5. Private access: restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file.

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

All entities in your code (with a few specific exceptions) have a default access level of internal.

7. Explain lazy in Swift ?

Lazy initialization — technique for delaying the creation of an object or some other expensive process until it’s needed. These properties can only use with class and struct member.Lazy properties are not thread safe because of not initialized automatically .

You must always declare a lazy property as a variable (with the var keyword).

Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

8. What is tuple in swift ?

A tuple is a group of zero or more values represented as one value. They are commonly used to return multiple values from a function call.

Tuple = Dictionary(can be created on the fly) + Struct (hold very specific types of data)

There are two types of Tuple in swift

1.Named Tuple

let nameAndAge = (name:”Midhun”, age:7)

Access the values like: //Output: Midhun

nameAndAge.age //Output: 7

In unnamed tuple we don’t specify the name for it’s elements.

let nameAndAge = (“Midhun”, 7)

Access the values like:

nameAndAge.0 //Output: Midhun

nameAndAge.1 //Output: 7

9. What is Enumeration ?

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Unlike C and Objective-C, Swift enumeration cases are not assigned a default integer value when they are created.

10. What is Associated Values?

Associated values are more like variables, associated with one of the enumeration cases.

enum Barcode {

case upc(Int, Int, Int, Int)

case qrCode(String)


“Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type (Int, Int, Int, Int), or a value of qrCode with an associated value of type String.”

It is sometimes useful to be able to store associated values of other types alongside case values.

This enables you to store additional custom information along with the case value, and permits this information to vary each time you use that case in your code.

Animesh Mishra

Written by

iOS and Unity 3D developer.