Skip to main content
InterviewHubPRO
Section Overview
Q10

Question 10 of 50

Part 1: SwiftUI Architecture, State & App Lifecycle (1 - 10)

20%
iOS Platform & SwiftUI (50 Qs)•Part 1: SwiftUI Architecture, State & App Lifecycle (1 - 10)

How do you implement Dependency Injection in iOS applications?

senior LevelMost AskedCode ExampleDependency InjectionLifecycleSystem Design

In Simple Words: Avoid hardcoding concrete dependencies inside classes:

  • Protocol-Based DI: Define an interface protocol (AuthServiceProtocol), inject it via the class initializer:
swift
protocol AuthServiceProtocol {
    func login(credentials: Credentials) async throws -> User
}

class LoginViewModel: ObservableObject {
    private let authService: AuthServiceProtocol

    init(authService: AuthServiceProtocol = AuthService()) {
        self.authService = authService
    }
}
  • Allows injecting mock services (MockAuthService) in Unit Tests with 100% test isolation.