Project 3: Expense Tracker with Charts

IOS Development 18 min min read Updated: Nov 16, 2026 Intermediate
Project 3: Expense Tracker with Charts
Intermediate Topic 3 of 4

What We Build

  • Add expenses with amount, category and date
  • List grouped by day with totals
  • Chart of spending by category
  • Data saved with SwiftData

1. Models

swift import SwiftData import Foundation enum ExpenseCategory: String, Codable, CaseIterable, Identifiable { case food = "Food", transport = "Transport", shopping = "Shopping", bills = "Bills", fun = "Fun" var id: Self { self } var symbol: String { switch self { case .food: "fork.knife" case .transport: "car.fill" case .shopping: "bag.fill" case .bills: "doc.text.fill" case .fun: "gamecontroller.fill" } } } @Model final class Expense { var title: String var amount: Double var date: Date var category: ExpenseCategory init(title: String, amount: Double, date: Date = .now, category: ExpenseCategory = .food) { self.title = title self.amount = amount self.date = date self.category = category } }

2. Main Screen

swift import SwiftUI import SwiftData import Charts struct ExpensesView: View { @Environment(\.modelContext) private var context @Query(sort: \Expense.date, order: .reverse) private var expenses: [Expense] @State private var showAdd = false private var total: Double { expenses.reduce(0) { $0 + $1.amount } } private var byCategory: [(category: ExpenseCategory, total: Double)] { Dictionary(grouping: expenses, by: \.category) .map { ($0.key, $0.value.reduce(0) { $0 + $1.amount }) } .sorted { $0.total > $1.total } } var body: some View { NavigationStack { List { Section { VStack(alignment: .leading, spacing: 12) { Text("Total spent").font(.subheadline).foregroundStyle(.secondary) Text(total, format: .currency(code: "USD")).font(.largeTitle.bold()) if !byCategory.isEmpty { Chart(byCategory, id: \.category) { item in SectorMark(angle: .value("Amount", item.total), innerRadius: .ratio(0.6), angularInset: 2) .foregroundStyle(by: .value("Category", item.category.rawValue)) .cornerRadius(4) } .frame(height: 200) } } .padding(.vertical, 8) } Section("History") { ForEach(expenses) { expense in HStack { Image(systemName: expense.category.symbol) .frame(width: 32) .foregroundStyle(.tint) VStack(alignment: .leading) { Text(expense.title) Text(expense.date, format: .dateTime.day().month()) .font(.caption).foregroundStyle(.secondary) } Spacer() Text(expense.amount, format: .currency(code: "USD")).bold() } } .onDelete { offsets in offsets.forEach { context.delete(expenses[$0]) } } } } .overlay { if expenses.isEmpty { ContentUnavailableView("No expenses yet", systemImage: "creditcard", description: Text("Tap + to add one.")) } } .navigationTitle("Expenses") .toolbar { Button("Add", systemImage: "plus") { showAdd = true } } .sheet(isPresented: $showAdd) { AddExpenseView() } } } }

3. Add Expense Form

swift struct AddExpenseView: View { @Environment(\.modelContext) private var context @Environment(\.dismiss) private var dismiss @State private var title = "" @State private var amount: Double? @State private var date = Date.now @State private var category: ExpenseCategory = .food var body: some View { NavigationStack { Form { TextField("Title", text: $title) TextField("Amount", value: $amount, format: .number) .keyboardType(.decimalPad) Picker("Category", selection: $category) { ForEach(ExpenseCategory.allCases) { Label($0.rawValue, systemImage: $0.symbol).tag($0) } } DatePicker("Date", selection: $date, displayedComponents: .date) } .navigationTitle("New expense") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } ToolbarItem(placement: .confirmationAction) { Button("Save") { context.insert(Expense(title: title, amount: amount ?? 0, date: date, category: category)) dismiss() } .disabled(title.isEmpty || (amount ?? 0) <= 0) } } } } }

4. Monthly Bar Chart

swift struct MonthlyChart: View { let expenses: [Expense] var body: some View { Chart(expenses) { expense in BarMark( x: .value("Month", expense.date, unit: .month), y: .value("Amount", expense.amount) ) .foregroundStyle(by: .value("Category", expense.category.rawValue)) } .chartYAxis { AxisMarks(format: .currency(code: "USD")) } .frame(height: 220) } }
Swift Charts: Describe marks (BarMark, LineMark, PointMark, SectorMark) and bind data to x, y or angle. Charts handles axes, legends and animations.
Ideas: Filter by month, set a budget with a progress bar, export to CSV with ShareLink, add a widget for today's spending.

Conclusion

You modeled data, built forms, grouped values and drew charts. Swift Charts turns arrays into polished visuals with very little code.

Quick Summary: Save expenses with SwiftData, group and sum with Dictionary(grouping:), and display with Chart, BarMark and SectorMark.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators