Sample CodeiOS, iPadOSReviewed 2026-07-21View on Apple Developer

TicTacFish: Implementing a game using distributed actors

At a glance

Item Summary
Purpose Use distributed actors to take your Swift concurrency and actor-based apps beyond a single process.
App architecture A Swift sample bundle with entry-bearing project variants Step 0 Offline Local Only, Step 1 Distributed yet still Local Bot, Step 2 Distributed Remote Bot, Step 3 Distributed Local Network Player, each leading to Distributed APIs.
Main patterns Model-View-ViewModel, Protocol-oriented abstraction, Publisher-backed observable state, Actor-isolated state
Project style 76 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── Step 2 Distributed Remote Bot/
│   ├── Server/
│   │   └── Sources/
│   │       └── TicTacFishServer/
│   │           └── boot.swift
│   ├── App/
│   │   └── TicTacFish/
│   │       └── TicTacFish/
│   │           └── TicTacFishApp.swift
│   └── Shared/
│       └── Sources/
│           └── TicTacFishShared/
│               ├── ActorSystems/
│               │   └── WebSocket/
│               │       └── WebSocketActorSystem.swift
│               └── ViewModel/
│                   └── GameViewModel.swift
├── Step 0 Offline Local Only/
│   ├── App/
│   │   └── TicTacFish/
│   │       └── TicTacFish/
│   │           ├── TicTacFishApp.swift
│   │           └── GameView.swift
│   └── Shared/
│       └── Sources/
│           └── TicTacFishShared/
│               └── ViewModel/
│                   └── GameViewModel.swift
├── Step 1 Distributed yet still Local Bot/
│   ├── App/
│   │   └── TicTacFish/
│   │       └── TicTacFish/
│   │           └── TicTacFishApp.swift
│   └── Shared/
│       └── Sources/
│           └── TicTacFishShared/
│               └── ViewModel/
│                   └── GameViewModel.swift
└── Step 3 Distributed Local Network Player/
    ├── App/
    │   └── TicTacFish/
    │       └── TicTacFish/
    │           └── TicTacFishApp.swift
    └── Shared/
        └── Sources/
            └── TicTacFishShared/
                ├── ActorSystems/
                │   └── LocalNetwork/
                │       └── LocalNetworkActorSystem.swift
                └── ViewModel/
                    └── GameViewModel.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 26 project/configuration file(s) and 111 source declaration(s).

Overall architecture

Reference code

Step 2 Distributed Remote Bot/Server/Sources/TicTacFishServer/boot.swift:17 — architecture anchor

@main
struct Boot {
    // ...
}

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

Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift:13 — stored dependency or nearest verified ownership anchor

struct GameView: View {
    // ...
    @StateObject private var model: GameViewModel
    // ...
}
Owner Object or state Relationship Mutation authority
GameView GameViewModel (model) owns wrapper-managed state Owning lexical scope
GameView GameMode (mode) stores or receives Initialized by the owner; the binding is immutable
GameView OfflinePlayer (player) stores or receives Initialized by the owner; the binding is immutable
GameView Model (_model) stores or receives App/module collaborators

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

Step 1 Distributed yet still Local Bot/Shared/Sources/TicTacFishShared/Game/Players.swift:42 — representative type boundary

public protocol GamePlayer: AnyActor, Identifiable where ID == ActorIdentity {
    // ...
}
Type Responsibility Depends on or conforms to
TicTacFishApp Application entry and top-level composition App
TicTacFishApp Application entry and top-level composition App
TicTacFishApp Application entry and top-level composition App
TicTacFishApp Application entry and top-level composition App
GamePlayer Defines a capability or collaboration contract AnyActor, Identifiable
GamePlayer Defines a capability or collaboration contract Sendable, Identifiable
GamePlayer Defines a capability or collaboration contract DistributedActor, Codable
WebSocketActorSystem Runs entity-component-system update logic DistributedActorSystem
WebSocketActorSystemResultHandler Handles callbacks or feature events DistributedTargetInvocationResultHandler
BonjourResultHandler Handles callbacks or feature events Distributed.DistributedTargetInvocationResultHandler

The source explicitly defines local protocol relationships: RandomPlayerBotAIPlayerBotAI, RandomPlayerBotAIPlayerBotAI, OfflinePlayerGamePlayer, RandomPlayerBotAIPlayerBotAI, OfflinePlayerGamePlayer, OnlineBotPlayerGamePlayer.

Access control

Symbol Access Verified effect Likely rationale
model (Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameFieldView.swift:13) 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.
model (Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift:13) 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.
GameView (Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift:41) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
makeMove (Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift:46) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameFieldView.swift:13 — representative boundary

struct GameFieldView: View {
    // ...
    @StateObject private var model: GameViewModel
    // ...
}

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 TicTacFishApp The source’s App suffix makes this role explicit.
Handles callbacks or feature events BonjourResultHandler, HTTPHandler, HTTPInitialRequestHandler, WebSocketActorMessageInboundHandler The source’s Handler suffix makes this role explicit.
Owns media or timeline playback BotPlayer, GamePlayer, LocalNetworkPlayer, OfflinePlayer The source’s Player suffix makes this role explicit.
Runs entity-component-system update logic WebSocketActorSystem The source’s System suffix makes this role explicit.
User-interface presentation and input forwarding GameFieldView, GameView, MainMenuView, TitleView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination GameViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel Step 0 Offline Local Only/Shared/Sources/TicTacFishShared/ViewModel/GameViewModel.swift:12 Role-named view models keep UI-facing state or coordination outside view declarations.
Protocol-oriented abstraction Step 0 Offline Local Only/Shared/Sources/TicTacFishShared/Game/PlayerBotAI.swift:23 A local protocol and concrete conformance create an explicit capability boundary.
Publisher-backed observable state Step 0 Offline Local Only/Shared/Sources/TicTacFishShared/ViewModel/GameViewModel.swift:22 Published properties notify observers while mutation remains with the state object.
Actor-isolated state Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift:34 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: TicTacFishApp; Handler: BonjourResultHandler, HTTPHandler, HTTPInitialRequestHandler, WebSocketActorMessageInboundHandler, WebSocketActorSystemResultHandler; Player: BotPlayer, GamePlayer, LocalNetworkPlayer, OfflinePlayer, OnlineBotPlayer; System: WebSocketActorSystem; View: GameFieldView, GameView, MainMenuView, TitleView; ViewModel: GameViewModel.
  • Protocols: GamePlayer, GamePlayer, GamePlayer, PlayerBotAI, PlayerBotAI, PlayerBotAI, PlayerBotAI, DistributedActorReceptionist.
  • Methods: main, syncShutdownGracefully, assignID, actorReady, resignID, resolve, resolveAny, makeInvocationEncoder.
  • Files: Step 0 Offline Local Only/App/TicTacFish/TicTacFish/TicTacFishApp.swift, Step 1 Distributed yet still Local Bot/App/TicTacFish/TicTacFish/TicTacFishApp.swift, Step 2 Distributed Remote Bot/App/TicTacFish/TicTacFish/TicTacFishApp.swift, Step 3 Distributed Local Network Player/App/TicTacFish/TicTacFish/TicTacFishApp.swift, Step 2 Distributed Remote Bot/Shared/Sources/TicTacFishShared/ActorSystems/WebSocket/WebSocketActorSystem.swift, Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift.

Architecture takeaways

  • boot is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches Distributed, SwiftUI, TicTacFishShared, NIOCore 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
Step 2 Distributed Remote Bot/Server/Sources/TicTacFishServer/boot.swift Boot
Step 0 Offline Local Only/App/TicTacFish/TicTacFish/TicTacFishApp.swift TicTacFishApp
Step 1 Distributed yet still Local Bot/App/TicTacFish/TicTacFish/TicTacFishApp.swift TicTacFishApp
Step 2 Distributed Remote Bot/App/TicTacFish/TicTacFish/TicTacFishApp.swift TicTacFishApp
Step 3 Distributed Local Network Player/App/TicTacFish/TicTacFish/TicTacFishApp.swift TicTacFishApp
Step 2 Distributed Remote Bot/Shared/Sources/TicTacFishShared/ActorSystems/WebSocket/WebSocketActorSystem.swift WebSocketWireEnvelope, RemoteWebSocketCallEnvelope, WebSocketActorSystemMode, WebSocketActorSystem, WebSocketActorSystemResultHandler, WebSocketActorSystemError
Step 3 Distributed Local Network Player/Shared/Sources/TicTacFishShared/ActorSystems/LocalNetwork/LocalNetworkActorSystem.swift BonjourResultHandler, SampleLocalNetworkActorSystemError, NetworkServiceConstants, Peer, RemoteCallEnvelope, ReplyEnvelope
Step 0 Offline Local Only/App/TicTacFish/TicTacFish/GameView.swift GameView, OfflinePlayer, GameView_Previews
Step 0 Offline Local Only/Shared/Sources/TicTacFishShared/ViewModel/GameViewModel.swift GameViewModel
Step 1 Distributed yet still Local Bot/Shared/Sources/TicTacFishShared/ViewModel/GameViewModel.swift GameViewModel