Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Swift Splash

At a glance

Item Summary
Purpose Use RealityKit to create an interactive ride in visionOS.
App architecture SwiftSplash composes WindowGroup + ImmersiveSpace around ContentView; AppState holds shared experience state and RealityKit components and systems own per-entity behavior.
Main patterns SwiftUI scene composition, SwiftUI–RealityKit bridge, Explicit immersive-space lifecycle, Observable state owner, Entity-component-system, Provider session boundary
Project style Code-rich sample with 37 scanned Swift file(s) and 4603 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
├── SwiftSplash/SwiftSplashApp.swift  # SwiftSplash
├── Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/MarkerComponents.swift  # PlaceStartPieceUIMarkerComponent, EditUILocationMarkerComponent, RideAnimationComponent
├── Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/ConnectableStateComponent.swift  # ConnectableStateComponent
├── Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/CustomBillboardComponent.swift  # CustomBillboardComponent
├── SwiftSplash/Views/ContentView.swift  # ContentView
├── SwiftSplash/Data & State/AppState.swift  # TilePieceKey, ConnectionPointType, AppState
├── Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/Connectable.swift  # ConnectableComponent
├── SwiftSplash/Views/TrackBuildingView.swift  # TrackBuildingView, AttachmentIDs
└── Packages/SwiftSplashTrackPieces/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + ImmersiveSpace; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is split into role-named views, models, managers, providers, components, or systems.
  • Authored .reality or Reality Composer Pro content is a real implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

SwiftSplash/SwiftSplashApp.swift:14 — the app or executable entry declares the outer scene lifecycle.

struct SwiftSplash: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

SwiftSplash/SwiftSplashApp.swift:19 — representative stored state or the nearest verified lifecycle anchor.

@main
@MainActor
struct SwiftSplash: App {
    // ...
    @State private var appState = AppState()
    // ...
}
Owner Object or state Relationship Mutation authority
SwiftSplash AppState as appState creates and retains Only the declaring scope writes
ContentView AppState as appState receives a shared/non-owning reference The upstream owner controls lifetime; this scope may invoke its mutable API
AppState ARKitSession as session creates and retains The owning type coordinates writes
SwiftSplash ImmersionStyle as immersionStyle owns SwiftUI value state Only the declaring scope writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
SwiftSplash (SwiftSplash/SwiftSplashApp.swift:14) Declares app scenes and top-level dependency lifetime. App
ContentView (SwiftSplash/Views/ContentView.swift:13) Presents UI and forwards gestures or lifecycle events. View
AppState (SwiftSplash/Data & State/AppState.swift:40) Owns observable feature state and domain transitions. Concrete framework collaborators
ConnectableComponent (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/Connectable.swift:14) Stores RealityKit entity data. Component, Codable
ConnectableStateComponent (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/ConnectableStateComponent.swift:18) Stores RealityKit entity data. Component, CustomStringConvertible
CustomBillboardComponent (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/CustomBillboardComponent.swift:12) Stores RealityKit entity data. Component, Codable
PlaceStartPieceUIMarkerComponent (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/MarkerComponents.swift:13) Stores RealityKit entity data. Component, Codable

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private let arkitSession = ARKitSession() (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:21) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.
public var entity: Entity? (Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/ConnectableStateComponent.swift:26) public The declaration is available to importing modules, subject to its containing type’s visibility. Inference: Export the type across the package-module boundary.

No reviewed declaration uses fileprivate, private(set), open; unmodified Swift declarations are internal.

Reference code

Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:21 — representative visibility boundary.

    private let arkitSession = ARKitSession()
    private let worldTrackingProvider = WorldTrackingProvider()
    
    public init(scene: RealityKit.Scene) {
        setUpSession()
    }

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime SwiftSplash The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures ContentView SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands AppState A role-named owner prevents sibling views from duplicating transitions.
Tracking authorization and update streams Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:21 Provider lifetime and async updates remain outside render-only view code.
Per-frame entity behavior Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:17 RealityKit systems query component data instead of centralizing every entity update in SwiftUI.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition SwiftSplash/SwiftSplashApp.swift:14 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
SwiftUI–RealityKit bridge SwiftSplash/Views/TrackBuildingView.swift:40 Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures.
Explicit immersive-space lifecycle SwiftSplash/SwiftSplashApp.swift:15 Makes immersive presentation a scene transition rather than hidden global state.
Observable state owner SwiftSplash/Data & State/AppState.swift:40 Shares feature state across multiple views without moving framework resources into view values.
Entity-component-system Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:17 Stores per-entity data in components and advances behavior in registered RealityKit systems.
Provider session boundary Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Systems/CustomBillboardSystem.swift:21 Owns provider lifetime separately from the SwiftUI view tree.

Naming conventions

  • Role suffixes are evidence, not decoration: View: ContentView, EditTrackPieceView, PieceShelfTrackButtonsView, PieceShelfView, PlaceStartPieceView.
  • ECS names pair data and behavior: components ConnectableComponent, ConnectableStateComponent, CustomBillboardComponent, PlaceStartPieceUIMarkerComponent; systems CustomBillboardSystem.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: the code-light sample has no separate command layer.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat SwiftSplash as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
  • Run ARKit providers for the scene lifetime and consume their asynchronous updates in cancellable tasks; map anchors to entities at the boundary.
  • Use RealityKit components for per-entity data and systems for repeated simulation instead of a monolithic view model.
  • Model immersive-space open, transition, and close states explicitly so windows and immersive content cannot drift apart.

Source map

Source file Relevant symbols
SwiftSplash/SwiftSplashApp.swift:14 SwiftSplash
Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/MarkerComponents.swift:13 PlaceStartPieceUIMarkerComponent, EditUILocationMarkerComponent, RideAnimationComponent, IdleAnimationComponent, RideWaterComponent, GlowComponent
Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/ConnectableStateComponent.swift:18 ConnectableStateComponent
Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/CustomBillboardComponent.swift:12 CustomBillboardComponent
SwiftSplash/Views/ContentView.swift:13 ContentView
SwiftSplash/Data & State/AppState.swift:19 TilePieceKey, ConnectionPointType, AppState, RideDestination
Packages/SwiftSplashTrackPieces/Sources/SwiftSplashTrackPieces/Components/Connectable.swift:14 ConnectableComponent
SwiftSplash/Views/TrackBuildingView.swift:15 TrackBuildingView, AttachmentIDs