A list of over 60 Swift and iOS questions asked during technical interviews.
Comments are welcome, including additional questions, alternative answers, and any corrections or clarifications.
Up-to-date for Swift 5.1
Swift and ObjC language
What’s Swift?
- A hybrid (function / object-oriented) modern language build to be concise and expressive
What’s Objective C?
- An object oriented language build on top of C
What’s the difference between Objective C and Swift?
- Objective C is an older language built on C, while Swift is a new, modern language with familiar syntax
What’s a class?
- A named reference type that defines properties and behaviors
What’s a struct?
- A named value type that groups properties and behavior
What’s an enum?
- Similar to structs, a named value type that groups data and behavior.
Also can contain a closed list of values (or types of values)
What’s the difference between a class and a struct?
- A class is a reference type while structs are value types. Classes also support inheritance while structs do not.
What’s a protocol?
- A blueprint of properties and behaviors that can be adopted by the other named types
What’s a tuple?
- A tuple is an unnamed, compound type that allows for grouping of zero or more other types
What is a typealias?
- A type identifier that allows you to give a name to an existing data type
How do you access elements in a tuple
- Unnamed tuples elements are referenced by dot notation (.0, .1)
Can classes/structs/enums conform to protocols?
- Yes
Can protocols define default behavior?
- No. Protocol extensions can.
What are protocol extensions?
- A pair of definitions. One is a protocol to define behaviors,
one is an extension of that protocol with implementation
Do classes have default initializers?
- Generally no. Classes without properties or with only mutable optional properties can use an empty initializer.
Do structs have default initializers?
- Yes
Can you override default initializers?
- Yes
Can you deinit a struct?
- No, it only applies reference types
When is deinit called
- Deinit runs when the reference count reaches 0 before deinitialization
What’s an extension?
- Adds behaviors to named types
Can structs/enums be extended?
- Yes
Do enums have default initializers?
- The enum cases are basically static initializers.
You can create init() functions (there’s no default) but they need to set self = case
Can do you iterate over all values of an enum?
- Make the enum conform to CaseIterable by adding it as declaration. The .allCases property becomes available with no extra code
What are associated values (in enums)
- An extra piece of information that can accompany an enum value. It must be passed in on initialization
Can let
class instances change values of their members?
- Yes, as long as those members are defined as vars
Can let
struct instances change values of their members?
- No.
What’s an associatedType?
- A generic placeholder that goes into protocols
What’s a generic?
- A mechanism for writing type-independent code
functions can be generic
What’s the difference between a typealias and generic?
- A typealias creates an alias for an existing type
An associatedType is used in Protocols in a similar way but doesn’t have to reference an existing type
What’s conditional conformance?
- A way to require / guarantee a Protocol or generic also conforms to another Protocol​
What’s an inout parameter?
- A function parameter that can be modified within the function
What’s a mutating function?
- A declaration that a function modifies the value types its in
What’s an attribute (@)
- A way of adding metadata to a function or property
What are some common attributes?
@available
tells the compiler which platform, version, or language a function requires@discardableResult
tells the compiler to suppress a warning@objc
exposes code to objective C
What’s a variadic parameter?
- A parameter type that is treated as an array but makes a function behave
as if can accept any number of parameters of the same types
What are In-Out parameters?
- Function parameters that can be modified within the function
What’s a delegate?
- A pattern where an instance of an object is set to handle certain behavior
iOS
What’s NotificationCenter?
- The mechanism to broadcast information (events) to subscribers
What’s ARC?
- Automatic Reference Counting is how Swift (and ObjC) tracks and manages memory usage.
What are weak / strong / unowned references?
- Ways of defining references to control how their references are counted.
What’s a retain cycle?
- When a reference count can never reach zero because of a cycle of strong references
How do you avoid a retain cycle?
- Ensure two classes never hold on to strong references to each other
What type inference?
- It’s how the compiler figures out what class type an object in when it’s not explicitly stated
What’s dynamic dispatch and static dispatch?
- Implementations of polymorphism where an implementation is chosen at run time or compile time.
What is method swizzing?
- Changing a selector at runtime; a capability of ObjectiveC but not Swift
What’s Any?
- A concrete type meant to represent any type, including optionals and functions
What’s AnyObject?
- AnyObject represents an instance of any class type
What is Foundation?
How are optionals implemented?
- As an enum of Some and None
What are Autoclosures?
- When a function takes a closure as a parameter, the autoclosure attribute can allow you to pass in a property thats acts like a closure that returns that property
Concurrency
What’s GCD?
- Grand Central Dispatch – the iOS mechanism for handling concurrency
What is DispatchQueue?
- The mechanism for storing and managing asynchronous operations
It has a Main queue (serial) meant to handle things like graphics
Then global queues for high, default, low, and background processing
How do you designate priority?
- By setting the QoS (Quality of Service)
What are OperationQueues?
- The mechanism for concurrency before GCD was around
Interface Builder
What’s AutoLayout?
- A constraint-based layout system that allows you to design one screen for multiple device sizes
What’s the difference between a view’s frame and its bounds?
- Both are CGRects representing the dimensions of a component
Bounds tend to start at 0, 0 and represents a component’s own coordinate system while frame is defined relative to its immediate parent
What does it mean if the bounds’ origin is not 0,0 or are not the same size as the frame?
- The bounds will not start at 0, 0 if it has been scrolled, and size will differ if it’s been zoomed.
(thanks to /u/clarkcox3)
UIKit
If a UITableView is choppy or loading slowly, what should you check?
- – Make sure a reuseIdentifier is defined and cells are being dequeued instead of initialized
– Make sure a cell’s configure method:
1) isn’t performing any unnecessary operations and
2) is loading/rendering any dynamic content asynchronously
CoreData
What’s necessary when using Managed classes?
Design Pattern
What’s MVVM?
- Model-View-ViewModel. A architectural design pattern, similar to MVC.
MVVM is often more appropriate In presentation-heavy apps, like web or mobile apps
Because it separates presentation logic and data from the viewControllers
What’s VIPER?
- An architectural pattern that separates View and Presenter, managed by a Router