Browse Questions
- URLCache stores responses from network requests in memory and/or on disk.
- A Singleton ensures only one instance of a class exists globally.
- The Factory pattern creates objects without exposing the creation logic.
- multipart/form-data is a content type used to send mixed data — like a file and text fields together — in a single request.
Answer: A Singleton ensures only one instance of a class exists globally. It's convenient but widely overused in iOS.
Code Example:
// Standard Swift singleton
class AnalyticsManager {
static let shared = AnalyticsManager()
private init() {} // prevent external instantiation
func track(_ event: String) { ... }
}
// Usage
AnalyticsManager.shared.track("button_tapped")
Downsides:
- Hard to test — can't inject a mock; tests share global state
- Hidden dependencies — callers depend on it without declaring it
- Threading issues — shared mutable state needs synchronization
- Tight coupling — callers are coupled to the concrete type
When it's acceptable:
- Logging, analytics, app-level config (read-only)
- Avoid for anything that manages data or has side effects