Suggest how I should lay out my files in a repo containing a SwiftUI MVVM app

Asked on 08/03/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

  1. Models: This directory contains your data models. For example, Pet.swift and Trick.swift represent the data structures for your app.

  2. 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, and TrickView.swift.

  3. ViewModels: This directory contains the view models for your app, which handle the business logic and data manipulation. For example, PetListViewModel.swift and PetDetailViewModel.swift.

  4. Resources: This directory contains resources like asset catalogs and localization files.

  5. App.swift: This is the entry point of your SwiftUI app.

  6. ContentView.swift: This is the main view of your app.

  7. Tests: This directory contains unit and UI tests for your app. Separate your tests into logical groups, such as MySwiftUIAppTests for unit tests and MySwiftUIAppUITests for UI tests.

Relevant Sessions

For more detailed guidance on SwiftUI and MVVM, you can refer to the following sessions from WWDC:

These sessions provide insights into organizing your SwiftUI code, understanding the fundamentals of views, and using containers effectively.