View on GitHub

swiftuivoodo

Simple architecture for the rest of us.

header

Simply Separated

There are only 3 parts:

  1. View
  2. Observable Object
  3. Data Object

Not all are required. And you can expand beyond these 3 for your app’s needs. Strive for simplicity.

V is for View

This is your SwiftUI view.

OO is for Observable object

This is a class that uses the @Observable macro.

DO is for Data Object

This is usually a struct that holds data.


Code Example

View

struct SettingsView: View {
    @State private var oo = SettingsOO()
    
    var body: some View {
        List(oo.data) { datum in
            Text(datum.name)
        }
        .task {
            oo.fetch()
        }
    }
}

Observable Object

import Observation

@Observable
class SettingsOO {
    var data: [DataObject] = []
    
    func fetch() {
        data = [DataObject(name: "Datum 1"),
                DataObject(name: "Datum 2"),
                DataObject(name: "Datum 3")]
    }
}

Data Object

struct DataObject: Identifiable {
    let id = UUID()
    var name: String
}

Preview

preview

Important Notes

You are in control!

in control

How is this different from other architectures?

The goal of architecture is to make your life (or your team’s life) easier. If this is not happening, then your architecture has failed.

This architecture is meant to:

Let’s talk about that last point…

Grow With Your Project - Example 1

  1. Minimum Viable Project
    1. Maybe you used that Xcode file template below and your view, observable object, and data object are in one file. Simple.
    2. You present your minimum viable product (MVP) to the world.
    3. People like it! You notice they’re asking for more features. 🙌
  2. Project Growth
    1. You add a new view. It uses the same data object as the previous view.
    2. Duplicating the same data object is a bad idea. So you create a folder called Data Objects and put it in its own file.
    3. Now both views and observable objects are using the same data object.

The idea is you use your judgment on how you want to break it out and organize it.

swiftui voodo architecture growth example 1

Let’s look at a different scenario.

Grow With Your Project - Example 2

  1. You decide to structure your project so the view, observable object, and data object are in separate files but all in the same folder so you can more easily work with all the related parts.
  2. Later, you find out that the MainDO (Main data object) can be reused in another view.
  3. So you create a CommonDataObjects folder and move MainDO.swift into it.

swiftui voodo architecture growth example 2

The choice is yours. You are in control!

Separate Folders Option

You can also start your project with a traditional separation of parts with separate folders:

swiftui voodo architecture growth example 3

In this scenario, Settings doesn’t need a data object so one isn’t created.

Remember the goal of architecture: To make your life easier.

Do what you have to with your architecture to keep making your life easier, not harder.

You are in control. Use your judgment.


Iterate Architecture Organization

Pick one VOODO folder/file strategy and try it. I would suggest picking what you think is the simplest one for yourself.

After a while, if you find yourself taking a long time moving between parts or having a hard time finding objects, then reorganize and simplify to make your life easier.

That is the goal of architecture: To make your life easier.


Resources

Working with Data in SwiftUI

Learn more about this architecture and working with data in the book SwiftUI Essentials: Architecting Scalable and Maintainable Apps.

swiftui essentials

Learn More

Xcode File Template

xcode file template

This file template will create all 3 objects when you add a new SwiftUI view to your project.

Download Here

Note: All 3 objects will be in one file. This is meant to be a STARTING POINT. Delete what you don’t need and separate out what you want OR keep everything in one file.

Remember, YOU are in control.