swift-observation

Documentation and usage patterns for Swift Observation, a framework that tracks changes to data and lets dependent code respond to them.

In plain words
What is it for?
Use it when making Swift or SwiftUI model data observable, tracking property changes, and replacing older ObservableObject-based code.
Why use it?
It shows how the @Observable macro can reduce repetitive code and update SwiftUI views only when properties they use change.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/steipete/agent-rules/swift-observation
Clone the repo
git clone --depth 1 https://github.com/steipete/agent-rules
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 3,447 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.03447
Opus 5 $0.00000 $0.01724
Sonnet 5 $0.00000 $0.00689
Haiku 4.5 $0.00000 $0.00345

Measured 2d ago against content hash 3ba20e8ca295, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

swift-observation scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

docs/swift-observation.mdc · 680 lines

How it starts

The opening of the file, as written. The whole thing — 680 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Swift Observation Framework

The Observation framework provides a robust implementation of the observer design pattern in Swift, introduced in iOS 17, macOS 14, tvOS 17, and watchOS 10 (Swift 5.9+).

Overview

The Observation framework centers around the @Observable macro, which automatically tracks changes to properties and notifies observers. This framework represents a significant improvement over the older ObservableObject protocol, offering better performance and a cleaner API.

Key Benefits

  • Fine-grained observation: SwiftUI views only update when observed properties actually change, not when any property on the object changes
  • Cleaner code: No need for @Published property wrappers on every property
  • Better performance: Views track dependencies at the property level, minimizing unnecessary view updates
  • Simpler syntax: Less boilerplate compared to ObservableObject

Using @Observable

To make a type observable, simply apply the @Observable macro to the class declaration:

import Observation

@Observable
class GameState {
    var score: Int = 0
    var level: Int = 1
    var playerName: String = ""

    // Private or computed properties work normally
    private var internalState: String = ""

    var displayName: String {
        "Player: \(playerName)"
    }
}

The @Observable macro automatically:

  • Adds conformance to the Observable protocol
  • Instruments property accessors to track reads and writes
  • Manages observer registration and notification

SwiftUI Integration

Using Observable Objects in Views

With @Observable, you use @State instead of @StateObject:

import SwiftUI

struct GameView: View {
    @State private var game = GameState()

    var body: some View {
        VStack {
            Text("Score: \(game.score)")
            Text("Level: \(game.level)")

            Button("Increase Score") {
                game.score += 10
            }
        }
    }
}

Important: Only the specific properties read in the view's body will trigger updates. If body only reads game.score, changes to game.level won't cause a re-render.

Read the full file on GitHub · 680 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 680 lines · 0 tokens per session scan A 3ba20e8ca295

Subscribe to this mod's changes

swift-observation is a cursor rule published in the GitHub repository steipete/agent-rules (5,695 stars, last pushed 4mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,447 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.