Interview Questions | Swift | Series — I

Catherine George
6 min readOct 12, 2022

--

Stuck in a rut? It’s time to level up your career!

I’m learning image
I am learning… Join me🥳

‘’One important key to success is self-confidence. An important key to self-confidence is preparation.’’

In this article, Let’s discuss some of the most common Swift interview questions and their answers. #swift #iOS

Difference between Any & AnyObject

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.
Venn diagram

Difference between weak & unowned reference

According to Apple’s Swift documentation,

  • weak reference doesn’t keep a strong hold on the instance it refers to, and so doesn’t stop ARC from disposing of the referenced instance. This behaviour prevents the reference from becoming part of a strong reference cycle.
  • unowned like weak reference, it doesn’t keep a strong hold on the instance it refers to. Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime.

Difference between guard let & if let statements

Despite of their similarities, both serve different purposes. The else case of the guard let exits the current scope and provides an early function return. if let nests its current scope inside the block, no return statement is required, however, it can be written.

Gif representation

Difference between == & === operators

  • Equality operator == checks whether two values are equal.
  • Identity operator === checks whether two references point to the same object instance.

Difference between upcasting & downcasting

Upcasting is typecasting of a child object to a parent object. Upcasting is done implicitly in most cases, So we don’t use the operator (as) frequently. Here is an example of upcasting Car to Vehicle

asOperator for upcasting.

let vehicle = Car() as Vehicle

Downcasting is typecasting of a parent class object to a child class object. Because the downcasting can fail, we can use the as operator in two different ways.

as? — Operator for optional downcasting | as! — Operator for forced downcasting

let car = vehicles.first as! Car // Forced Downcasting
let car = vehicles.first as? Car // Optional Downcasting

Difference between raw value & associated value in enumerations

According to Apple’s Swift documentation,

  • rawValue can be strings, characters, or any of the integer or floating-point number types. Each raw value must be unique within its enumeration declaration.
  • associatedValue are values of any type to be stored along with each different case value in an enumeration.

Difference between map & compact map

According to Apple’s Swift documentation,

  • map() returns an array containing the results of mapping the given closure over the sequence’s elements.
  • compactMap() returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

We can use map() to transform the collection based on the mapping closure, and the function will return an array of transformed elements. Similarly to map(), compactMap() also returns an array of transformed array elements, but the difference is that it will only return non-nil array elements.

Difference between self & Self

self refers to the instance itself, whereas Self refers to the type of instance.

https://developer.apple.com/forums/thread/5479

# Difference between value type & reference type in Swift

According to Apple’s Swift documentation,

  • Value type — Each instance keeps a unique copy of its data, usually defined as a struct, enum, or tuple.
  • Reference type — Instances share a single copy of the data, and the type is usually defined as a class.
Gif representation of value type & reference type

⭐️ Bonus point — Describe How to choose

Use a value type (eg: struct) when:

  • Comparing instance data with == makes sense
  • You want copies to have independent state
  • The data will be used in code across multiple threads

Use a reference type (eg: class) when:

  • Comparing instance identity with === makes sense
  • You want to create shared, mutable state

Difference between struct & class

The significant difference is

  • Structs are value types and stored in stack memory
  • Classes are reference types and stored in heap memory

According to Apple’s Swift documentation,

Structures and classes in Swift have many things in common. Both can

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

Classes have additional capabilities that structures don’t have:

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.

Difference between implicitly unwrapped & force unwrapped

Unlike regular optionals, implicitly unwrapped optionals might contain a value or be nil, but they don’t need to be unwrapped before use. We can implicitly unwrap an optional by adding an exclamation mark after the type name.

var name: String!

By force unwrapping an optional, Swift will convert it to a non-optional type from an optional type. We can force unwrap an optional by adding an exclamation mark while we use the optional. However, force unwrapping is unsafe, as a nil value will result in a crash.

let string = "5"
let number = Int(string)!

Difference between open & public access specifiers

open access specifier only applies to classes and class members. It allows code outside the module to subclass and override.

public - Similarly to open, The public access specifier allows code outside the module but doesn’t allow it to subclass and override.

Pictorial representation

Difference between content hugging & compression resistance

contentHuggingPriority - Content hugging priority resists the view growing more than its intrinsic content size.

contentCompressionResistancePriority - Compression resistance resists the view shrinking smaller than its intrinsic content size.

Pictorial representation of content hugging & compression resistance

Difference between Array & Set

According to Apple’s Swift documentation,

  • Array is an ordered, random access collection of same type.
  • Set is an unordered collection of unique elements.

⭐️ Bonus point

In set, the time complexity of contains() method is constant time O(1) independent of data size.

In array, the time complexity of contains() method is linear time O(n).

Difference between static method & class method

Both static and class methods are associated with its type rather than the instance. Subclasses can override class methods, but not static methods.

⭐️ Bonus point

static methods dispatch statically, which allows compile-time optimization.

Difference between an escaping & non-escaping closure

According to Apple’s Swift documentation,

  • escaping - A closure that can escape from the function block if it is passed as an argument and called after the function returns.
  • non-escaping - A closure that can only remain alive inside the function block So it should be called before the function finishes execution.

⭐️ Bonus point

Swift 3 onwards, closures are non-escaping by default. Unlike non-escaping, the escaping closures can be alive in memory even after the function returns unless it is a weak reference.

Thanks for Reading! ✌️

I have come up with these questions from my personal experiences. Hope you find this article helpful🥳. If you have any questions or corrections, please leave a comment below.

--

--

Catherine George
Catherine George

Written by Catherine George

iOS Developer 👩‍💻| plant-lover🪴| craftswoman 🎁| writing about iOS, swift & interview questions 📝

Responses (1)