What is the Singleton pattern and give an iOS example?
Ensures only one instance of a class exists. Example: UIApplication.shared, UserDefaults.standard.
What is the downside of using Singleton in iOS?
Global state, makes testing harder, can cause hidden dependencies.
Code sample of singleton pattern
final class Singleton {
// 1. Shared static instance (lazy + thread-safe)
static let shared = Singleton()
// 2. Private initializer prevents external initialization
private init() {}
// Example method
func doSomething() {
print("Singleton instance is working.")
}
}What is the Observer pattern and give an iOS example?
Defines a one-to-many dependency so observers are notified of changes. Example: NotificationCenter, KVO, Combine publishers.
Sample code for notification Center
import Foundation
// Sender
class Poster {
func postMessage() {
NotificationCenter.default.post(name: .myNotification, object: nil)
}
}
// Receiver
class Observer {
init() {
NotificationCenter.default.addObserver(
self,
selector: #selector(handleNotification),
name: .myNotification,
object: nil
)
}
@objc private func handleNotification() {
print("Notification received!")
}
}
// Usage
extension Notification.Name {
static let myNotification = Notification.Name("MyNotification")
}
let observer = Observer()
let poster = Poster()
poster.postMessage() // prints: Notification received!
What is the Delegation pattern and how is it used in iOS?
One object delegates responsibility to another via a protocol. Example: UITableViewDelegate, UITextFieldDelegate.
What is the Factory Method pattern and give an iOS example?
Provides an interface for creating objects but lets subclasses decide the type. Example: UIButton(type: .system).
What is the Adapter pattern and give an iOS example?
Converts one interface into another expected by clients. Example: Using NSFetchedResultsController to adapt Core Data results for UITableView.
What is MVC in iOS architecture?
Model-View-Controller: Apple’s classic architecture, but can lead to ‘Massive View Controller’.
What is MVVM and how does it improve over MVC?
Model-View-ViewModel separates presentation logic into the ViewModel, reducing controller size. Common with Combine/SwiftUI.
What is the Strategy pattern and give an iOS example?
Defines a family of interchangeable algorithms. Example: Sorting arrays with different closures.
What is the Command pattern and give an iOS example?
Encapsulates an action as an object. Example: NSUndoManager commands for undo/redo.
What is the Facade pattern and give an iOS example?
Provides a simplified interface to a complex subsystem. Example: URLSession as a facade over CFNetwork.
What is the Iterator pattern and give an iOS example?
Provides a way to access elements of a collection sequentially without exposing internal representation. Example: Swift Sequence and IteratorProtocol.
What is the Composite pattern and give an iOS example?
Treats individual objects and compositions uniformly. Example: UIView containing subviews in UIKit.
Why is Singleton commonly used in iOS frameworks?
To provide global shared resources like UIApplication, FileManager, UserDefaults without multiple instances.
How can you make a Singleton thread-safe in Swift?
By using a static constant which is lazily initialized and guaranteed to be thread-safe in Swift.
What is the difference between NotificationCenter and KVO?
NotificationCenter broadcasts to many observers; KVO observes property changes on specific objects.
How is the Observer pattern used in SwiftUI?
Via @Published properties and ObservableObject, which notify SwiftUI views automatically.
Why is Delegation preferred over NotificationCenter in UIKit?
Delegation is one-to-one and enforces compile-time contracts via protocols, making it safer.
How does Delegation reduce coupling?
By defining behavior in a protocol, the delegate class can change without modifying the delegating class.
What is the advantage of Factory Method in Swift?
It encapsulates object creation, makes code more flexible and testable, avoids spreading init details.
Give an example of Factory Method in Foundation or UIKit.
URLRequest(url:) creates a request; UIButton(type:) creates a button with style.
When would you use the Adapter pattern in iOS?
When you want to integrate a legacy API with a new interface, e.g., adapting old Objective-C APIs for SwiftUI.