Explain it simply
Mini Project, in plain language
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.
Problem → Why → Solution
The problem — why this concept matters
Leaving totalLessonCount at 0 while calculating a percentage can cause a division-by-zero issue. In a real project, you should add validation for this.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
Build Mini Project in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- typed value
- Swift expression or function
- safe result
Explain the type, ownership, and runtime behavior of Mini Project
Core syntax or pattern
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)")
}Build Mini Project in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
Read the code step by step
- typed value
struct CourseProgress {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.
- Swift expression or function
}Build Mini Project in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- safe result
}Run and test an original Mini Project example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- typed value
- Swift expression or function
- safe result
Success
Explain the type, ownership, and runtime behavior of Mini Project
Failure
Leaving totalLessonCount at 0 while calculating a percentage can cause a division-by-zero issue. In a real project, you should add validation for this.
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.
Real-world usage
How to apply it in a real project
Run and test an original Mini Project example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Mini Project example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
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)")
}Expected: Explain the type, ownership, and runtime behavior of Mini Project
Show hint
Run and test an original Mini Project example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed value → Swift expression or function → safe result
- Build Mini Project in vertical slices that separate UI, domain state, persistence or services, and tests. Cover normal, boundary, nil, error, cancellation, and concurrency paths without blocking main-actor work.
- Leaving totalLessonCount at 0 while calculating a percentage can cause a division-by-zero issue. In a real project, you should add validation for this.
- Run and test an original Mini Project example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org