Folder structure

Guide for understanding folder structure of SwiftSail


SwiftSail comes with a well thoughtout folder structure to keep the codebase clean and easy to understand.

├── Data
   ├── Datasources
   ├── ...datasource swift files
   ├── Repositories
   ├── ...repositories swift files
├── Domain
   ├── Interactors
   ├── Models
   ├── Protocols
├── Presentation
   ├── Components
   ├── Extensions
   ├── Generated
   ├── Resources
├── Features
   ├── Analytics
   ├── Auth
   ├── Dashboard
   ├── View models
   ├── Views
   ├── Onboarding
   ├── Purchases
   ├── Settings
├── AppDelegate.swift
├── AppRouter.swift
├── AppDependencies.swift
└── ...other files

SwiftSail follows the basic principles of clean architecture. It's split into three main layers: data, domain and presentation.

Data

This layers serves as a concrete implementation of all datasources and repositories. Datasource can be anything that provides data to your app such as SwiftData, remote ApiError, Firebase, RevenueCat, and so on. This layer also contains repositories that presents an interface between your app and datasource. Repositories handle all communication with datasource and the app doesn't care about the datasource.

Domain

Domain layer is the core of your app. It contains the business logic and in ideal world it should be platform agnostic. The same business logic you write in Swift should easily be rewritten to other languages and inserted into web app, android app, and so on.

It's important to keep this layer completely separate so it doesn't care about any APIs, libraries, UI,...

Domain layer is a place for your domain models (not data models), interactors and protocols. Interactor is usually used in view models and contains more complex business logic. Protocols are "templates" for your repositories which are implemented in the data layer.

Presentation

This layer contains common UI components used across the app. You can find all UI components in the Components folder. It also contains all resources such as fonts, images and colors.

IMPORTANT: All resources (fonts, images, colors) are generated using SwiftGen library. You can find the generated code in the Generated folder.

Features

This folder contains all features your app has. Each feature has its own UI views and view models. Keep in mind that this folder is just a helper folder to better separate different sections of the app. In case of some bigger features that requires own architecture, you might want to create separate folders for that feature and split in the same folder structure Data, Domain, Presentation.

AppRouter.swift

This is the main router of the app and is responsible for connecting everything together. It decides on what screen to display and what action to do based on delegate patterns.

AppDependencies.swift

This file is the main source of all dependencies. In SwiftSail, Dependency Injection is heavily used to allow for quick replacing of datasources or other APIs.

If you have any questions about this folder structure feel free to reach to me on Twitter