Getting Started
As an iOS developer, you know that acing your job interview is crucial for landing your dream job. To help you prepare for your next interview, we’ve compiled a list of some of the most commonly asked questions in iOS interviews. In this two-part series, we’ll cover a wide range of topics, from foundational concepts to advanced techniques. Whether you’re a seasoned developer looking to brush up on your skills or a beginner just starting out, this list of frequently asked questions will help you feel confident and ready to ace your iOS interview. So let’s get started!
Easy Questions & Answers
Question: What’s an Optional in Swift?
Answer: Optionals are swift’s way of handling nullable variables. Optionals are an important concept in Swift because they allow us to safely handle the absence of a value and avoid runtime errors related to trying to access a value that does not exist
Question: What is Double Question mark in Swift?
Answer: The nil coalescing operator. It’s a way for us to assign a default value to an optional variable in swift.
Question: What is Optional Chaining?
Answer: Optional Chaining in swift, is a way for us to retrieve a value from a chain of optional values. It allows us to fail gracefully when we try to access the property of an object that is nil. It’s basically a way of saying everything after the question marks will only run if everything before the question marks has value.
Question: What is Optional Binding in Swift?
Answer: is a simple of safely unwrapping
optional values within a given scope.
Question: What is a lazy variable?
Answer: A lazy stored property is a property whose initial value isn’t calculated until the first time it’s used.
Caveat: a computed property can’t be lazy property
Question: Class Vs Struct
Answer: Classes are reference types, while structs are value types. This means any changes to a class object’s reference will affect other references of the object.
A way to explain this eloquently is to compare a reference type to a google doc shared with a coworker and a value type to a copy of a resume you emailed to a coworker. Any changes that my coworker makes on the google doc (reference type) will subsequently affect my own version of the google doc, since they are fundamentally the same object. Meanwhile any changes my coworker makes on the document i emailed her will not affect my own version of the document, since they are fundamentally different objects.
Also classes empowers us with other important oop things like polymorphisms, inheritance etc. And they (reference types) also are generally responsible for a lot of memory leaks and memory issues in ios apps. Classes are dynamically dispatched, while structs are statically dispatched.
Dynamic dispatch happens at runtime while static dispatch happens at compile time
Question: What is Delegate vs Notifications?
Answer: Both are communication patterns in ios. The Delegate protocol pattern is generally a 1 to 1 communication pattern, while NotificationCenters are 1 to many communication pattern.
Question: What is an Enum?
Answer: an enum is a group of related values. And as you can tell from that statement it’s a value type that empowers us with the ability to write clean, case iterable code in ios.
Enums have two ways of defining values:
- Raw value
- Associated value; that we pass as a parameter into the enum case
Question: What is Guard statement in swift?
Answer: Guard statements are a way to provide an early exist from a scope when a specific condition is not met. And like optional bindings empower us with the ability to safely unwrap optional variables.
Question: What is Defer Statement in swift?
Answer: The Defer statement is a way for us to execute a block of code before we exit the current scope no matter how that exit happens.
It’s important note that defer statements are executed after the return and they are executed no matter how we exist a scope
Question: What is ARC?
Answer: Automatic Reference Count is apple’s way of handling memory management for us under the hood. What it does is that when you instantiate a reference type, for example a class object, ARC will remember how many times that object is being referenced. And will increment and decrement that reference count accordingly. This is important because that object will only be de-allocated from memory when it’s ARC < 1.
Note: The object won’t be de-allocated from memory even if you explicitly set it to nil if it’s ARC > 0. The solution to this is weak and unowned references.
Question: What’s a weak reference?
Answer: A weak reference in swift is one that does not increase or decrease the automatic reference count of an object. Essentially it doesn’t protect the object from being de-allocated from memory. It’s a great way to prevent retain cycles.
Question: What’s a unowned reference?
Answer: Like a weak reference, an unowned reference does not increment or decrement the reference count of an object. However, unlike a weak reference, we are guaranteeing the program that the unowned referenced object will not be nil when it is accessed.
Question: Difference between weak self and unowned self?
Answer: weak self creates an optional variable that can be automatically de-allocated from memory by ARC when there’s no more strong reference to the object. While unowned self doesn’t create an optional variable because we are explicitly telling the program that the object will be available and never be nill during the current scope, so for whatever reason if the object is nill when accessed it will crash the app.
Question: When would you use one over the other?
Answer: Use unowned reference when you know for sure that the object will never be nil during the current scope’s lifecycle. If not stick with weak reference, since its safer and prevents unnecessary crashes that can be caused by trying to access an object that’s already been de-allocated.
Question: What is ARC, and how does it work?
Answer: ARC, or Automatic Reference Counting, is a memory management feature in iOS that automatically tracks and manages the lifetime of objects in your application. It keeps track of the number of references to an object, and when that number reaches zero, the object is de-allocated from memory. This allows you to focus on the logic of your application, without having to worry about manually managing memory.
To be continued in part 2 ✌️….