Sample CodeiOS, iPadOS, Mac Catalyst, watchOSReviewed 2026-07-21View on Apple Developer

Tracking heart rate zones for workouts

At a glance

Item Summary
Purpose Start a workout on iOS or watchOS and track and display heart rate zones.
App architecture A Swift sample bundle with entry-bearing project variants HKWorkoutZoneSample Watch App, HKWorkoutZoneSampleApp, each leading to HealthKit APIs.
Main patterns Delegate or data-source callbacks, Actor-isolated state
Project style 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── HKWorkoutZoneSample Watch App/
│   ├── HKWorkoutZoneSampleApp.swift
│   ├── MetricsView.swift
│   ├── ControlsView.swift
│   ├── SessionView.swift
│   └── StartView.swift
├── HKWorkoutZoneSampleApp/
│   ├── HKWorkoutZoneSampleApp.swift
│   └── MetricsView.swift
└── Shared/
    ├── Views/
    │   ├── SummaryView.swift
    │   ├── ElapsedTimeView.swift
    │   └── HeartRateZoneView.swift
    ├── WorkoutManager.swift
    └── NavigationModel.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 5 project/configuration file(s) and 30 source declaration(s).

Overall architecture

Reference code

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:10 — architecture anchor

@main
struct HKWorkoutZoneSampleWatchApp: App {
    // ...
}

Interpretation

The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.

Ownership and state

Ownership evidence

Shared/Views/SummaryView.swift:123 — stored dependency or nearest verified ownership anchor

private struct ZoneDetailView: View {
    let title: String
    // ...
}
Owner Object or state Relationship Mutation authority
ZoneDetailView String (title) owns value state Initialized by the owner; the binding is immutable
ZoneDetailView ZoneDisplayData (zones) stores or receives Initialized by the owner; the binding is immutable
SummaryMetricRow String (title) owns value state Initialized by the owner; the binding is immutable
SummaryMetricRow String (value) owns value state Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Class and protocol design

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:11 — representative type boundary

struct HKWorkoutZoneSampleWatchApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
HKWorkoutZoneSampleWatchApp Application entry and top-level composition App
HKWorkoutZoneSampleApp Application entry and top-level composition App
SummaryView User-interface presentation and input forwarding View
ZoneDetailView User-interface presentation and input forwarding View
WorkoutManager Long-lived feature or framework coordination NSObject
MetricsView User-interface presentation and input forwarding View
MetricsView User-interface presentation and input forwarding View
NavigationModel Feature data or observable state Concrete collaborators/imported frameworks
ElapsedTimeView User-interface presentation and input forwarding View
HeartRateZoneView User-interface presentation and input forwarding View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
workoutManager (HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:12) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
workoutManager (HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.swift:12) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
timerFinishedSubject (Shared/CountDownManager.swift:16) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
countDownConnector (Shared/CountDownManager.swift:21) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:12 — representative boundary

@main
struct HKWorkoutZoneSampleWatchApp: App {
    @State private var workoutManager = WorkoutManager.shared
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition HKWorkoutZoneSampleApp, HKWorkoutZoneSampleWatchApp The source’s App suffix makes this role explicit.
Long-lived feature or framework coordination CountDownManager, WorkoutManager The source’s Manager suffix makes this role explicit.
Feature data or observable state MetricsModel, NavigationModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, ControlsView, CountDownView, ElapsedTimeView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Delegate or data-source callbacks Shared/WorkoutManager.swift:225 Callback protocols invert event delivery back into the sample’s owner.
Actor-isolated state Shared/CountDownManager.swift:11 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: HKWorkoutZoneSampleApp, HKWorkoutZoneSampleWatchApp; Manager: CountDownManager, WorkoutManager; Model: MetricsModel, NavigationModel; View: ContentView, ControlsView, CountDownView, ElapsedTimeView, HeartRateZoneView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: zoneColor, formatZoneDuration, prepareWorkout, requestAuthorization, startWorkout, pause, resume, togglePause.
  • Files: HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.swift, Shared/Views/SummaryView.swift, Shared/WorkoutManager.swift, HKWorkoutZoneSample Watch App/MetricsView.swift, HKWorkoutZoneSampleApp/MetricsView.swift, Shared/NavigationModel.swift.

Architecture takeaways

  • HKWorkoutZoneSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, HealthKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift HKWorkoutZoneSampleWatchApp
HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.swift HKWorkoutZoneSampleApp
Shared/Views/SummaryView.swift SummaryView, ZoneDetailView, SummaryMetricRow
Shared/WorkoutManager.swift ZoneDisplayData, WorkoutManager, SessionStateChange
HKWorkoutZoneSample Watch App/MetricsView.swift MetricsView, MetricRow
HKWorkoutZoneSampleApp/MetricsView.swift MetricsView, CustomAlignment
Shared/NavigationModel.swift NavigationModel, NavigationState
Shared/Views/ElapsedTimeView.swift ElapsedTimeView, ElapsedTimeFormatter
Shared/Views/HeartRateZoneView.swift HeartRateZoneView, ZoneBarChart
HKWorkoutZoneSample Watch App/ControlsView.swift ControlsView