Member-only story
Organizing iOS Files Based on the Clean Architecture
Separation of Concerns
In any iOS application, knowing what should go where is an essential way of organizing code and making sure our code is maintainable for the future.
How you should organize the key components and files within an iOS application then?
Here is a roadmap of how I think you could organize files within your application, and maintain a separation of concerns that can improve maintainability within any given iOS application. Based on the clean architecture here is the organization system you might use if creating an all-new greenfield application.
View
Purpose
Handles UI rendering and displaying content. In SwiftUI, it would be a struct conforming to View. In UIKit, it could be UIView subclasses.
Responsibilities
Display data passed from the ViewModel or ViewController.
Handle user interactions (e.g., button taps) and forward those to the ViewModel or ViewController.
Should not contain business logic.
Example Files
MovieListView.swift
(SwiftUI)
CustomButtonView.swift
(UIKit)
ViewController (UIKit)
Purpose
Manages the lifecycle of views, binds data from ViewModel (in MVVM) or directly controls the views (in MVC), and initiates data fetching from the ViewModel or Interactor.
Responsibilities
Manage the display logic (setting up views, handling view states).
Respond to user interactions (e.g., handling actions like taps or gestures).
Initiate data fetching by interacting with the ViewModel or Interactor.
Manage transitions between different screens (though this can be offloaded to a Coordinator).
SwiftUI
There is no ViewController in SwiftUI. Some of the ViewController responsibilities in UIKit are handled in the view when using SwiftUI. In essence, SwiftUI folds the responsibilities of the…