Simply Separated
There are only 3 parts:
- View
- Observable Object
- 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.
- It may or may not show any data.
- In SwiftUI, if you want to change the way your view looks, you will have to change some data.
- Data can be within the view. Data can be within an Observable object.
OO is for Observable object
This is a class that uses the @Observable macro.
- It can be used to notify your view when data values change.
- When data changes, your view will update.
- Data can be simple, like a string or int. Data can also be an object, like a struct.
DO is for Data Object
This is usually a struct that holds data.
- You can have one data object in your Observable object.
- You can have an array of data objects in your Observable object.
- Your Observable can send data objects to your view to be displayed.
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
Important Notes
You are in control!
- You decide on the naming convention you want to use.
- You can update the Xcode file template below to fit your needs.
- You decide how simple or how complex this needs to be.
- Don’t need the Data Object? Delete it!
- Want to put the observable object and view in the same file? Go for it!
- Want all 3 in separate files but in the same folder? Why not?
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:
- Provide a starting point
- Be flexible
- Be as simple or as complicated as you need
- Have fewer rules
- Allow the developer to use their judgment
- Grow with your project.
Let’s talk about that last point…
Grow With Your Project - Example 1
- Minimum Viable Project
- Maybe you used that Xcode file template below and your view, observable object, and data object are in one file. Simple.
- You present your minimum viable product (MVP) to the world.
- People like it! You notice they’re asking for more features. 🙌
- Project Growth
- You add a new view. It uses the same data object as the previous view.
- Duplicating the same data object is a bad idea. So you create a folder called
Data Objects
and put it in its own file. - 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.
Let’s look at a different scenario.
Grow With Your Project - Example 2
- 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.
- Later, you find out that the
MainDO
(Main data object) can be reused in another view. - So you create a
CommonDataObjects
folder and moveMainDO.swift
into it.
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:
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.
Xcode File Template
This file template will create all 3 objects when you add a new SwiftUI view to your project.
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.