iOS Source Code Structure
iOS Source Code Structure
Overview: A summary of the standard iOS project file structure, key application files, and common architectural patterns used in modern iOS development.
๐งญ High-Level Project Structure
A well-organized iOS project separates concerns into clear, distinct directories. While small apps might start with a flat structure, a modern, scalable iOS app (often using MVVM or Clean Architecture) typically follows a structure like this:
flowchart LR
A[๐ MyApp]
A --> B[๐ App]
B --> B1[๐ AppDelegate.swift]
B --> B2[๐ SceneDelegate.swift]
A --> C[๐ Presentation]
C --> C1[๐ Views]
C --> C2[๐ ViewControllers]
C --> C3[๐ ViewModels]
A --> D[๐ Domain]
D --> D1[๐ Models / Entities]
D --> D2[๐ UseCases]
A --> E[๐ Data]
E --> E1[๐ Networking]
E --> E2[๐ Repositories]
A --> F[๐ Core]
F --> F1[๐ Utilities]
F --> F2[๐ Extensions]
A --> G[๐ Resources]
G --> G1[๐ผ Assets.xcassets]
G --> G2[๐ Info.plist]
A --> H[๐ Tests]
H --> H1[๐ฌ Unit Tests]
H --> H2[๐ฌ UI Tests]
๐ Key Files Explained
Every iOS project comes with a set of default files critical carefully managing the appโs configuration and lifecycle.
| File / Component | Purpose |
|---|---|
AppDelegate.swift | Handles app-level lifecycle events (e.g., app launch, push notifications registration, background fetch). |
SceneDelegate.swift | Introduced in iOS 13, handles UI-level lifecycle events (foreground, background) and supports multi-window setups. |
MyApp.swift (SwiftUI) | The @main entry point for pure SwiftUI apps, replacing the need for AppDelegate/SceneDelegate in many cases. |
Info.plist | Property List file containing configuration data (bundle ID, version), and user permissions (e.g., Camera, Location access). |
Assets.xcassets | Centralized repository for organizing images, color sets, and app icons. |
LaunchScreen.storyboard | The initial screen displayed immediately by the OS while the app is loading into memory. |
๐ Common Architecture Patterns
How you structure your code within the folders depends heavily on the chosen architecture:
- MVC (Model-View-Controller)
- Appleโs default pattern.
- Pros: Simple, built into Cocoa Touch.
- Cons: Often leads to โMassive View Controllerโ because ViewControllers end up handling networking, routing, and data formatting.
- MVVM (Model-View-ViewModel)
- Highly popular in modern iOS, especially with SwiftUI.
- Pros: Extracts presentation logic from the View/ViewController into a ViewModel. Easily testable. Relies on data binding (via
Combine,RxSwift, or SwiftUIโs@State/@Published).
- VIPER / Clean Architecture
- View, Interactor, Presenter, Entity, Router.
- Pros: Extremely decoupled, clear boundaries, highly testable.
- Cons: High boilerplate; over-engineering for small or simple apps.
๐ฆ Dependency Management
When building an iOS app, you often rely on external libraries. These are managed via:
- Swift Package Manager (SPM): Appleโs native, modern dependency manager. Integrated directly into Xcode. Uses
Package.swift(or Xcode UI). (Highly Recommended) - CocoaPods: The most established 3rd-party manager. Uses a
Podfileand generates an.xcworkspacethat you must use instead of the.xcodeproj. - Carthage: A decentralized dependency manager that builds your dependencies into binary frameworks and leaves integrating them up to you.
โ Best Practices for iOS Project Structure
- Modularization: As projects grow, break code down into reusable Swift Packages or local frameworks (e.g., separating the
Networklayer from theUIlayer). - Separation of Concerns: Keep ViewControllers and Views entirely free of business logic and network requests.
- Resource Code Generation: Use tools like SwiftGen to auto-generate type-safe references for your
Assets.xcassets, Strings, and Storyboards to avoid string-typing crashes. - Group by Feature vs Type: For medium-to-large apps, grouping folders by Feature (e.g.,
Profile/,Login/,Home/) rather than by Type (e.g.,ViewModels/,Views/) often makes navigation easier.
This post is licensed under CC BY 4.0 by the author.