I have two structs in my task manager app:
struct Task {
var identifier: String
var title: String
var tags: [String] // Array of tag identifiers
}
struct Tag {
var identifier: String
var title: String
}
I then have a class to store them:
class TaskStore: ObservableObject {
@Published var tasks = [String:Task]()
@Published var tags = [String:Tag]()
}
which I pass to my root view as an .environmentObject(taskStore)
.
Correct me if any of the following is wrong (against bad practices):
In my TaskView
I have:
@EnvironmentObject var taskStore: TaskStore
var taskIdentifier: String // Passed from parent view
private var task: Task {
get {
return taskStore.tasks[taskIdentifier]! // Looks up the task in the store
}
}
private var tags: [Tag] {
get {
return taskStore.tags
}
}
The issue is, when learning SwiftUI I was told when making certain components (like a picker that let's you alter the tags array in this case) that it should accept a binding to the value/collection, or say I want to make the task title editable, I need a binding to the task.title
property, both of which I can't do because (based on the way I'm defining and computing task
) I can't get a binding on task
.
Am I doing something against best practices here? Or where along this path did I diverge from the right way of storing points of truth in an environment object, and make them editable in sub views.