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.