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

WWDC22 Challenge: Learn Switch Control through gaming

At a glance

Item Summary
Purpose Play a card-matching game using Switch Control.
App architecture A Swift sample with the source-visible chain SwitchControlChallengeAppContentViewSwiftUI / UIKit APIs.
Main patterns Publisher-backed observable state
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task, await suspension point; none alone proves a background thread.
State/event model Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, Foundation, Combine, UIKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── SwitchControlChallenge/
│   ├── SwitchControlChallengeApp.swift
│   ├── Views/
│   │   ├── ContentView.swift
│   │   ├── GameBoardView.swift
│   │   ├── CardView.swift
│   │   └── Intro/
│   │       ├── IntroPage.swift
│   │       └── DescriptionPoint.swift
│   └── Models/
│       ├── Card.swift
│       ├── Deck.swift
│       ├── Game.swift
│       └── Symbol.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── SwitchControlChallenge.xcodeproj/
    └── .xcodesamplecode.plist

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 3 project/configuration file(s) and 10 source declaration(s).

Overall architecture

Reference code

SwitchControlChallenge/SwitchControlChallengeApp.swift:10 — architecture anchor

@main
struct SwitchControlChallengeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Accessibility.

Ownership and state

Ownership evidence

SwitchControlChallenge/Views/GameBoardView.swift:14 — stored dependency or nearest verified ownership anchor

struct GameBoardView: View {
    // ...
    @StateObject var viewModel = Game(numberOfCards: Self.numberOfCards)
    // ...
}
Owner Object or state Relationship Mutation authority
GameBoardView Game (viewModel) owns wrapper-managed state App/module collaborators
CardView Card (card) stores or receives Initialized by the owner; the binding is immutable
Card UUID (id) owns value state Initialized by the owner; the binding is immutable
Card Symbol (symbol) stores or receives 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.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. SwitchControlChallenge/Models/Game.swift:42
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. SwitchControlChallenge/Models/Game.swift:42
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. SwitchControlChallenge/Models/Game.swift:42
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. SwitchControlChallenge/Models/Game.swift:44

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

SwitchControlChallenge/Models/Game.swift:42 — representative execution boundary

                Task { @MainActor in
                    score += 1
                    try await Task.sleep(nanoseconds: 1_000_000_000)
                    UIAccessibility.post(notification: .announcement, argument: "Found a match!")
                    deck.cards[requestedCardIndex].isMatched = true
                    deck.cards[currentlySelectedCardIndex].isMatched = true
                }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation ObservableObject ObservableObject supplies an observation contract. SwitchControlChallenge/Models/Game.swift:11
State propagation @Published A published property can emit owner-controlled changes. SwitchControlChallenge/Models/Game.swift:13
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. SwitchControlChallenge/Views/GameBoardView.swift:14
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. SwitchControlChallenge/Models/Symbol.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. SwitchControlChallenge/Models/Card.swift:8
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. SwitchControlChallenge/Models/Game.swift:8
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. SwitchControlChallenge/Models/Game.swift:9

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

SwitchControlChallenge/SwitchControlChallengeApp.swift:11 — representative type boundary

@main
struct SwitchControlChallengeApp: App {
    // ...
            ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
SwitchControlChallengeApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
GameBoardView User-interface presentation and input forwarding View
CardView User-interface presentation and input forwarding View
Card Represents a feature value or composable behavior Identifiable
Deck Represents a feature value or composable behavior Concrete collaborators/imported frameworks
Game Owns feature behavior and collaborator lifecycle ObservableObject
Symbol Defines a closed set of feature states or choices String, RawRepresentable, CaseIterable
IntroPage Represents a feature value or composable behavior View
DescriptionPoint Represents a feature value or composable behavior Identifiable

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
deck (SwitchControlChallenge/Models/Game.swift:13) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
currentlySelectedCardIndex (SwitchControlChallenge/Models/Game.swift:19) 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.
numberOfCards (SwitchControlChallenge/Models/Game.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.
numberOfPairs (SwitchControlChallenge/Models/Game.swift:22) 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

SwitchControlChallenge/Models/Game.swift:13 — representative boundary

class Game: ObservableObject {
    // ...
    @Published private(set) var deck: Deck
    // ...
}

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 SwitchControlChallengeApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding CardView, ContentView, GameBoardView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Publisher-backed observable state SwitchControlChallenge/Models/Game.swift:13 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: SwitchControlChallengeApp; View: CardView, ContentView, GameBoardView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: shuffle, getCard, toggle, isMatch, select, reset, randomSymbol, symbolSet.
  • Files: SwitchControlChallenge/SwitchControlChallengeApp.swift, SwitchControlChallenge/Views/ContentView.swift, SwitchControlChallenge/Views/GameBoardView.swift, SwitchControlChallenge/Views/CardView.swift, SwitchControlChallenge/Models/Card.swift, SwitchControlChallenge/Models/Deck.swift.

Architecture takeaways

  • SwitchControlChallengeApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, UIKit 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
SwitchControlChallenge/SwitchControlChallengeApp.swift Cited implementation, SwitchControlChallengeApp
SwitchControlChallenge/Views/GameBoardView.swift Cited implementation, SwiftUI state property wrapper, GameBoardView, GameBoardView_Previews
SwitchControlChallenge/Models/Game.swift Cited implementation, @MainActor, Task closure isolated to MainActor, Task, await suspension point, ObservableObject, @Published, Combine, UIKit, Game
SwitchControlChallenge/Models/Symbol.swift SwiftUI, Symbol
SwitchControlChallenge/Models/Card.swift Foundation, Card
SwitchControlChallenge/Views/ContentView.swift ContentView, ContentView_Previews
SwitchControlChallenge/Views/CardView.swift CardView
SwitchControlChallenge/Models/Deck.swift Deck
SwitchControlChallenge/Views/Intro/IntroPage.swift IntroPage, IntroPage_Previews
SwitchControlChallenge/Views/Intro/DescriptionPoint.swift DescriptionPoint