I have the following problem:
I have an application, which uses two tableViewControllers. The first TableViewController lists
struct Task{
let type: String
[other attributes]
}
These Tasks are stored in a TaskStore class, with a static shared instance which stores all Tasks in the application.
class TaskStore {
var tasks: [Task] = []
class var sharedInstance: TaskStore {
struct Static {
static let instance = TaskStore()
}
return Static.instance
}
}
Now, for each Task, I need to store a set of Subtasks associated to that instance of Task. Hence, I created a Subtask struct
struct Subtask{
let subtype: String
[other attributes]
}
and created a SubtaskStore class, which I modified to no longer include the static instance, but instead:
class SubtaskStore {
var subtasks: [Subtask] = []
class var sharedInstance: SubtaskStore {
let instance = SubtaskStore()
return instance
}
}
Note: This seems to me to be much more beneficial than simply including an array of Subtasks as a member of the Task struct, because I can create methods that operate of the SubtaskStore.sharedinstance.
Then I amended the Task struct as follows
struct Task{
let type: String
var subtasks: SubtaskStore // as oppose to: var subtasks [Subtask] = []
}
The idea is that now every Task has its own SubtaskStore, which stores all the Subtasks for the Task at hand.
First question: Is there a better way of modeling the hierarchy of Tasks to Subtasks? I am quite new to programming, but the design seemed intuitive.
I have successfully created an interactive TableViewController that displays the elements in TaskStore and updates the Table when a new Task is added/removed. I have now created a link between the Task TableViewController and the SubTask TableViewController; clicking on a TaskTableViewCell opens a new SubTaskTableViewController (currently empty).
Second question: Assume that the TaskTableViewCell selected by the user is in row 5. I want to display the elements in "TaskStore[4].subtasks" in the SubtaskTableViewController. How do I pass the number "4" from the selected TaskTableViewCell in the TaskTableViewController to the SubTaskTableViewController and display on the elements in the TaskStore[4].subtasks.sharedInstace?