All skills
johnrogers avatar

/swiftui-advanced

@d62eb71

Use when implementing gesture composition (simultaneous, sequenced, exclusive), adaptive layouts (ViewThatFits, AnyLayout, size classes), or choosing architecture patterns (MVVM vs TCA vs vanilla, State-as-Bridge). Covers advanced SwiftUI patterns beyond basic views.

Use this Skill: https://skilld.dev/gh/johnrogers/claude-swift-engineering/swiftui-advanced

This session only. Nothing lands on disk.

referencesperformance.md

≈677 tokens on demand. Your agent reads this file only when SKILL.md points to it.

SwiftUI Performance

Core Principle

Ensure view bodies update quickly and only when needed.

Two Problems

  1. Long View Body Updates - Body takes too long
  2. Unnecessary Updates - Views update when data hasn't changed

SwiftUI Instrument (Instruments 26)

  1. Press Cmd-I in Xcode
  2. Choose SwiftUI template
  3. Check Long View Body Updates lane (red = priority)

Problem 1: Long Updates

Formatter Creation

// WRONG - creates every render
var body: some View {
    let formatter = NumberFormatter()
    Text(formatter.string(from: price)!)
}

// CORRECT - cache formatters
class Formatters {
    static let currency: NumberFormatter = {
        let f = NumberFormatter()
        f.numberStyle = .currency
        return f
    }()
}

Complex Calculations

// WRONG
var body: some View {
    Text("\(data.sorted().last ?? 0)")
}

// CORRECT - compute in model
@Observable class ViewModel {
    var data: [Int] { didSet { maxValue = data.max() ?? 0 } }
    private(set) var maxValue = 0
}

Synchronous I/O

// NEVER
var body: some View {
    let data = try? Data(contentsOf: url)
}

// CORRECT
.task { data = try? await loadData() }

Problem 2: Unnecessary Updates

Many small updates add up to miss frame deadline.

Shared Dependencies

// WRONG - all views depend on whole array
func isFavorite(_ item: Item) -> Bool {
    favorites.contains(item)  // Depends on entire array
}

// CORRECT - per-item view models
@Observable class ItemViewModel { var isFavorite = false }

class ModelData {
    var itemViewModels: [ID: ItemViewModel] = [:]
}

Environment Values

// WRONG - updates 60x/second
.environment(\.scrollOffset, offset)

// CORRECT - pass directly
ChildView(scrollOffset: offset)

iOS 26 Automatic Wins

Rebuild with iOS 26 SDK:

  • 6x faster list loading (100k+ items)
  • 16x faster list updates
  • Reduced dropped frames
  • Nested ScrollView lazy loading

30-Minute Diagnostic Protocol

Step Time
Build Release 5 min
Trigger issue 3 min
Record trace 5 min
Review Long Updates 5 min
Check Cause & Effect 5 min
Identify view 2 min

Before Shipping a Fix

  • Ran SwiftUI Instrument?
  • Know which view is expensive?
  • Can explain why fix helps?
  • Verified in Instruments?

Key Patterns

Per-item dependencies:

// Each view depends only on its model
@Observable class ItemViewModel { var item: Item }

Formatter reuse:

static let dateFormatter: DateFormatter = { ... }()

Cached computations:

var data: [Int] { didSet { cached = compute(data) } }

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill is a collection of reference documentation and code patterns for advanced SwiftUI development. No security issues, malicious code, or suspicious instructions were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    5/5 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d62eb71. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for johnrogers/claude-swift-engineering/swiftui-advanced