Jeffrey Hicks

Jeffrey Hicks

Platform Eng @R360

SwiftUI Project Structure Based on Apple Guidance

Apple's recommended folder structure and architectural patterns for SwiftUI applications based on official sample projects

By Jeffrey Hicks • Aug 20, 2025 • compiled-notes

Core Architecture: App, Scene, and View

Apple’s fundamental SwiftUI architecture revolves around three building blocks:

1. App Protocol

  • The entry point of your SwiftUI application
  • Contains and manages scenes
  • Uses the @main attribute to mark the app’s entry point

2. Scene Protocol

  • Represents distinct regions of your app’s UI
  • Most commonly implemented as WindowGroup for standard apps
  • Other scene types include DocumentGroup for document-based apps and Settings for macOS preferences

3. View Protocol

  • The basic building blocks that render everything on screen
  • Can be composed together to form complex user interfaces

Based on Apple’s sample projects (Backyard Birds and Food Truck), here’s the recommended structure:

MyApp/
├── MyApp.swift                    # App entry point with @main
├── ContentView.swift              # Main content view
├── Assets.xcassets               # Image and color assets
├── Info.plist                    # App configuration
├── Features/                     # Feature-based organization
│   ├── Home/
│   │   ├── HomeView.swift
│   │   ├── HomeViewModel.swift   # If using MVVM
│   │   └── Components/
│   ├── Profile/
│   │   ├── ProfileView.swift
│   │   └── ProfileEditView.swift
│   └── Settings/
├── Models/                       # Data models
│   ├── User.swift
│   └── AppData.swift
├── Services/                     # Network, persistence, etc.
│   ├── NetworkService.swift
│   └── DataStore.swift
├── Shared/                       # Reusable components
│   ├── Views/
│   ├── Modifiers/
│   └── Extensions/
└── Resources/                    # Additional resources

Multi-Platform Structure

For apps targeting multiple platforms, Apple demonstrates this approach in their Food Truck sample:

MyApp/
├── Multiplatform/               # Shared code across platforms
│   ├── MyApp.swift
│   ├── Navigation/
│   ├── Features/
│   └── Assets.xcassets
├── MyAppKit/                    # Reusable framework/package
│   └── Sources/
│       ├── Models/
│       ├── Services/
│       └── Extensions/
├── Widgets/                     # Widget extension
└── WatchApp/                    # watchOS app

Key Principles from Apple’s Guidance

1. Model-View (MV) Pattern

Apple’s SwiftUI samples demonstrate a simplified architecture where:

  • Views directly observe models using @Observable (iOS 17+) or ObservableObject
  • No intermediate ViewModels are required for simple cases
  • State management uses @State, @StateObject, and @Binding

2. Data Essentials

  • Use @State for view-local state
  • Use @StateObject for reference type ownership
  • Use @ObservedObject for observing external objects
  • Use @EnvironmentObject for dependency injection

3. Container and Presenter Pattern

  • Container views handle data fetching and state
  • Presenter views focus on displaying data
  • This separation ensures reusability

Best Practices from Apple’s Implementation

Group files by feature rather than by type (views, models, etc.) to improve discoverability and maintainability.

2. Use Swift Packages for Reusable Code

Both sample apps demonstrate using Swift Package Manager for shared code:

  • BackyardBirdsData - Data layer package
  • BackyardBirdsUI - UI components package
  • FoodTruckKit - Shared business logic

3. Leverage SwiftUI’s Built-in Architecture

  • Don’t over-engineer with unnecessary layers
  • Use SwiftUI’s reactive data flow naturally
  • Avoid creating ViewModels when direct model observation suffices

4. Platform-Specific Code Organization

Use conditional compilation and platform-specific folders when needed:

#if os(iOS)
    // iOS-specific code
#elseif os(macOS)
    // macOS-specific code
#endif

Sample Implementation

Here’s a minimal example following Apple’s pattern:

// MyApp.swift
@main
struct MyApp: App {
    @StateObject private var dataStore = DataStore()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(dataStore)
        }
    }
}

// Models/DataStore.swift
@MainActor
class DataStore: ObservableObject {
    @Published var items: [Item] = []
    
    func loadData() async {
        // Load data
    }
}

// Features/Home/HomeView.swift
struct HomeView: View {
    @EnvironmentObject var dataStore: DataStore
    
    var body: some View {
        NavigationStack {
            List(dataStore.items) { item in
                ItemRow(item: item)
            }
        }
        .task {
            await dataStore.loadData()
        }
    }
}

Architecture Comparison

Traditional iOS Architecture

Traditional MVC/MVVM:
┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│    Model    │◄──►│ View Model  │◄──►│    View     │
└─────────────┘    └─────────────┘    └─────────────┘

Apple’s SwiftUI MV Pattern

SwiftUI MV Pattern:
┌─────────────┐    ┌─────────────┐
│ @Observable │◄──►│    View     │
│    Model    │    │  (SwiftUI)  │
└─────────────┘    └─────────────┘

Project Organization Tips

Feature Modules

Organize by user-facing features rather than technical layers:

Features/
├── Authentication/
│   ├── LoginView.swift
│   ├── SignUpView.swift
│   └── AuthenticationService.swift
├── Dashboard/
│   ├── DashboardView.swift
│   ├── DashboardViewModel.swift
│   └── Components/
│       ├── MetricCard.swift
│       └── ChartView.swift
└── Settings/
    ├── SettingsView.swift
    └── PreferencesStore.swift

Shared Components

Create reusable UI components:

Shared/
├── Views/
│   ├── CustomButton.swift
│   ├── LoadingView.swift
│   └── ErrorView.swift
├── Modifiers/
│   ├── CardStyle.swift
│   └── AnimationModifiers.swift
└── Extensions/
    ├── View+Extensions.swift
    └── Color+Theme.swift

Testing Structure

Mirror your main structure in tests:

Tests/
├── UnitTests/
│   ├── Models/
│   ├── Services/
│   └── ViewModels/
├── UITests/
│   ├── Features/
│   └── Flows/
└── TestUtilities/
    ├── MockData.swift
    └── TestHelpers.swift

Key Takeaways

Apple’s recommended structure emphasizes:

  1. Simplicity First: Start with basic MV pattern, add complexity only when needed
  2. Feature-Based Organization: Group related files together by user-facing features
  3. Swift Package Manager: Use packages for reusable code across projects
  4. Built-in State Management: Leverage SwiftUI’s reactive data flow naturally
  5. Platform Awareness: Organize code to handle multi-platform scenarios effectively

Conclusion

Apple’s recommended structure emphasizes simplicity, feature-based organization, and leveraging SwiftUI’s built-in state management rather than imposing complex architectural patterns. The key is to start simple and add complexity only when needed, keeping your code organized by features and making use of Swift packages for reusable components.

This approach aligns perfectly with SwiftUI’s declarative nature and Apple’s design philosophy of making development more intuitive and maintainable.

References

  1. SwiftUI App Structure
    https://developer.apple.com/documentation/swiftui/app

  2. WWDC 2020: SwiftUI App Structure
    https://developer.apple.com/videos/play/wwdc2020/10037/

  3. Building Large Scale Apps with SwiftUI
    https://azamsharp.com/2023/02/28/building-large-scale-apps-swiftui.html

  4. SwiftUI Architecture: Complete Guide to MV Pattern
    https://betterprogramming.pub/swiftui-architecture-a-complete-guide-to-mv-pattern-approach-5f411eaaaf9e

  5. SwiftUI Architecture Guide
    https://www.swiftyjourney.com/swiftui-architecture-a-complete-guide-to-the-mv-pattern-approach

  6. SwiftUI Data Flow (YouTube)
    https://www.youtube.com/watch?v=V2yKZHrXRYA

  7. Managing User Interface State
    https://developer.apple.com/documentation/swiftui/managing-user-interface-state

  8. SwiftUI Documentation
    https://developer.apple.com/documentation/SwiftUI

  9. Exploring SwiftUI App Structure
    https://developer.apple.com/tutorials/swiftui-concepts/exploring-the-structure-of-a-swiftui-app

  10. SwiftUI Tutorials
    https://developer.apple.com/tutorials/swiftui/

  11. SwiftUI App Organization
    https://developer.apple.com/documentation/swiftui/app-organization

  12. SwiftUI Get Started
    https://developer.apple.com/swiftui/get-started/

  13. iOS Project Standards
    https://github.com/BottleRocketStudios/iOS-Project-Standards/blob/main/Project%20Structure/Project%20Structure.md

  14. Xcode Project Structure (YouTube)
    https://www.youtube.com/watch?v=E3DOCoZ7oH8

  15. Xcode Project Groups Best Practice
    https://stackoverflow.com/questions/39945727/best-practice-for-an-xcode-project-groups-structure

  16. SwiftUI Official Architecture Discussion
    https://forums.swift.org/t/what-is-the-architecture-officially-recommended-by-apple-for-swiftui-applications/44930

  17. WWDC 2020: Data Essentials in SwiftUI
    https://developer.apple.com/videos/play/wwdc2020/10040/

  18. SwiftUI Scene Documentation
    https://developer.apple.com/documentation/swiftui/scene

  19. Nice Architecture for SwiftUI
    https://steamclock.com/blog/2024/04/nice-architecture

  20. MVVM in SwiftUI
    https://www.avanderlee.com/swiftui/mvvm-architectural-coding-pattern-to-structure-views/

  21. MV State Pattern Introduction
    https://azamsharp.com/2022/08/09/intro-to-mv-state-pattern.html

  22. WWDC 2024: SwiftUI Essentials
    https://developer.apple.com/videos/play/wwdc2024/10150/

  23. Backyard Birds Sample Project
    https://developer.apple.com/documentation/swiftui/backyard-birds-sample

  24. Backyard Birds GitHub
    https://github.com/apple/sample-backyard-birds

  25. Food Truck Sample Project
    https://developer.apple.com/documentation/swiftui/food_truck_building_a_swiftui_multiplatform_app

  26. Food Truck GitHub
    https://github.com/apple/sample-food-truck