ArchitectureMidMCQ
What is Clean Architecture in iOS?
Test your knowledge:
Explanation & Code
Answer: Clean Architecture organises code into concentric layers where dependencies only point inward. The inner layers have no knowledge of the outer layers.
[ UI / Presentation ]
↓
[ Use Cases / Interactors ]
↓
[ Domain / Entities ]
↑
[ Data / Repositories ] (implements interfaces defined in Domain)
Key rule: The Domain layer has zero imports of UIKit, SwiftUI, or any framework.
Code Example:
// Domain — pure Swift, no framework imports
struct Article { let id: String; let title: String }
protocol ArticleRepository {
func fetchAll() async throws -> [Article]
}
class FetchArticlesUseCase {
private let repo: ArticleRepository
init(repo: ArticleRepository) { self.repo = repo }
func execute() async throws -> [Article] { try await repo.fetchAll() }
}
// Data layer — implements the protocol
class RemoteArticleRepository: ArticleRepository { ... }
// Presentation layer — uses the use case
class ArticleViewModel: ObservableObject {
private let useCase: FetchArticlesUseCase
@Published var articles: [Article] = []
func load() async { articles = try await useCase.execute() }
}
Rate your understanding: