In this mini project we'll combine Swift basics to build a Course Progress Tracker. It's an example that calculates a learner's lesson progress using arrays, dictionaries, functions, optionals, loops, and conditions.
This little project gives you a logic foundation you can reuse in real apps for things like course dashboards, onboarding checklists, task trackers, and habit trackers.
struct CourseProgress {
let learnerName: String
var completedLessons: [String]
let totalLessonCount: Int
var completedCount: Int {
completedLessons.count
}
var percent: Double {
Double(completedCount) / Double(totalLessonCount) * 100
}
func summary() -> String {
if completedLessons.isEmpty {
return "\(learnerName) has not started yet."
}
return "\(learnerName) completed \(completedCount)/\(totalLessonCount) lessons (\(Int(percent))%)."
}
}
var progress = CourseProgress(
learnerName: "Nandar",
completedLessons: ["Intro", "Variables", "Arrays"],
totalLessonCount: 10
)
print(progress.summary())
for lesson in progress.completedLessons {
print("Done: \(lesson)")
}CourseProgress struct stores the learner's name, completed lessons, and total lesson count. completedCount and percent are computed properties. summary() method returns the progress status as user-facing text.
Nandar completed 3/10 lessons (30%). Done: Intro Done: Variables Done: ArraysWhat's Next
As a next step, add an addCompletedLesson() method and check that a new lesson isn't added twice. Then try displaying this data in a SwiftUI List view.