Let's think about this for a second
In this mini project we'll build a Task Manager app that manages a to-do-style task list. We'll split it into 3 parts, and in Part 1 we'll lay down the data model and core structure first. We'll define a Task struct as a value type holding title, isDone, and priority for each task, and use a Priority enum for priority levels. Since TaskManager needs to hold the task list and handle add/print operations, we'll write it as a class — a reference type. This is hands-on practice directly applying the struct-vs-class and enum topics.
Let's build it
Build a Priority enum with three cases: low, medium, high. Give the Task struct four fields: id (Int), title (String), priority (Priority), isDone (Bool, default false). Build a TaskManager class with a private var tasks: [Task] = [] array, and write an addTask(title:priority:) method that auto-generates the id using tasks.count + 1. Write a printAllTasks() method that formats and prints each task with its id, title, priority, and status. In Part 2, we'll keep building on this TaskManager by adding filter/sort features.
Sample Code
enum Priority: String {
case low = "Low"
case medium = "Medium"
case high = "High"
}
struct Task {
let id: Int
var title: String
var priority: Priority
var isDone: Bool = false
}
class TaskManager {
private var tasks: [Task] = []
func addTask(title: String, priority: Priority) {
let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
tasks.append(newTask)
}
func printAllTasks() {
print("----- Task List -----")
for task in tasks {
let status = task.isDone ? "Done" : "Pending"
print("#\(task.id) [\(task.priority.rawValue)] \(task.title) - \(status)")
}
}
}
let manager = TaskManager()
manager.addTask(title: "Swift optionals ပြန်ကျက်မယ်", priority: .high)
manager.addTask(title: "Struct vs Class notes ရေးမယ်", priority: .medium)
manager.printAllTasks()The console prints a task list header followed by two neatly formatted lines containing id, priority, title, and status.Give it 5 minutes
Add an urgent case to Priority, add about three tasks with addTask, then call printAllTasks() and check whether the output format is correct. Spend about 5 minutes on it.
One thing to watch out for
Keep the Task struct as a value type — understanding that appending it to an array makes a copy will make writing the update logic in Part 2 much easier.