Suggest how I should lay out my files in a repo containing a SwiftUI MVVM app
Generated on 8/3/2024
1 search
To lay out your files in a repository containing a SwiftUI MVVM app, you can follow a structured approach that aligns with best practices. Here’s a suggested layout:
Suggested Repository Structure
MySwiftUIApp/
├── MySwiftUIApp/
│ ├── Models/
│ │ ├── Pet.swift
│ │ └── Trick.swift
│ ├── Views/
│ │ ├── PetListView.swift
│ │ ├── PetDetailView.swift
│ │ └── TrickView.swift
│ ├── ViewModels/
│ │ ├── PetListViewModel.swift
│ │ └── PetDetailViewModel.swift
│ ├── Resources/
│ │ ├── Assets.xcassets
│ │ └── Localizable.strings
│ ├── App.swift
│ └── ContentView.swift
├── Tests/
│ ├── MySwiftUIAppTests/
│ │ ├── PetListViewModelTests.swift
│ │ └── PetDetailViewModelTests.swift
│ └── MySwiftUIAppUITests/
│ └── PetListViewUITests.swift
├── README.md
└── LICENSE
Explanation
-
Models: This directory contains your data models. For example,
Pet.swift
andTrick.swift
represent the data structures for your app. -
Views: This directory contains your SwiftUI views. Each view corresponds to a screen or a component in your app, such as
PetListView.swift
,PetDetailView.swift
, andTrickView.swift
. -
ViewModels: This directory contains the view models for your app, which handle the business logic and data manipulation. For example,
PetListViewModel.swift
andPetDetailViewModel.swift
. -
Resources: This directory contains resources like asset catalogs and localization files.
-
App.swift: This is the entry point of your SwiftUI app.
-
ContentView.swift: This is the main view of your app.
-
Tests: This directory contains unit and UI tests for your app. Separate your tests into logical groups, such as
MySwiftUIAppTests
for unit tests andMySwiftUIAppUITests
for UI tests.
Relevant Sessions
For more detailed guidance on SwiftUI and MVVM, you can refer to the following sessions from WWDC:
- SwiftUI essentials (Fundamentals of views)
- A Swift Tour: Explore Swift’s features and design (Code organization)
- Demystify SwiftUI containers (Composition)
These sessions provide insights into organizing your SwiftUI code, understanding the fundamentals of views, and using containers effectively.

SwiftUI essentials
Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Platforms State of the Union
Discover the newest advancements on Apple platforms.

Demystify SwiftUI containers
Learn about the capabilities of SwiftUI container views and build a mental model for how subviews are managed by their containers. Leverage new APIs to build your own custom containers, create modifiers to customize container content, and give your containers that extra polish that helps your apps stand out.

Migrate your TVML app to SwiftUI
SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.