There are only 3 parts:
- View
- ObservableObject
- DataObject
Not all are required. And you can expand beyond these 3 for your apps needs. Strive for simplicity.
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 ObservableObject.
This is a class that inherits from ObservableObject.
- 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.
This is usually a struct that holds data.
- You can have one data object in your ObservableObject.
- You can have an array of data objects in your ObservableObject.
- Your ObservableObject can send data objects to your view to be displayed.
struct SettingsView: View {
@StateObject private var oo = SettingsOO()
var body: some View {
List(oo.data) { datum in
Text(datum.name)
}
.onAppear {
oo.fetch()
}
}
}
class SettingsOO: ObservableObject {
@Published var data: [DataObject] = []
func fetch() {
data = [DataObject(name: "Datum 1"),
DataObject(name: "Datum 2"),
DataObject(name: "Datum 3")]
}
}
struct DataObject: Identifiable {
let id = UUID()
var name: String
}
- 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 the same folder? Why not?
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.
- 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...
- 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.
- 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!
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.
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.
Learn more about this architecture and working with data in the book Working with Data in SwiftUI.
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.