Project Overview
Part of the Logic & Flow section of Coddy's Swift journey — lesson 33 of 56.
You're going to build a tiny habit tracker. State lives in a single dictionary mapping a habit name to its history of done days for the week:
var habits: [String: [Int]] = [
"read": [1, 3, 5], // done on Mon, Wed, Fri
"workout": [2, 4] // done on Tue, Thu
]Days are integers 1...7 for Mon...Sun. Across the next four lessons you'll add features one at a time:
- Add a habit and mark it done
- Compute streaks across the week
- Print a weekly grid
- Compare two weeks
Each lesson stands alone, you'll re-declare the dictionary at the top of every file. The state is the same shape so the methods you write transfer across lessons.
Challenge
BeginnerTo kick off the project, write the data setup code:
- Initialize an empty
habitsdictionary ([String: [Int]]) - Print the count of habits
- Print whether the dictionary is empty
The expected output is:
0
trueCheat sheet
A habit tracker can be modeled as a dictionary mapping habit names to arrays of completed day numbers (1...7 for Mon–Sun):
var habits: [String: [Int]] = [
"read": [1, 3, 5], // done on Mon, Wed, Fri
"workout": [2, 4] // done on Tue, Thu
]Useful dictionary properties:
habits.count // number of entries
habits.isEmpty // true if no entriesTry it yourself
// TODO: declare an empty [String: [Int]], then print count and isEmpty
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
Count and IndicesCase and TrimSearching in StringsSplitting and JoiningReplacing SubstringsRecap - Username Check