SwiftUI Pro

repository·main·Indexed 26 days ago

https://github.com/twostraws/swiftui-agent-skill

An agent skill for AI coding assistants (Claude Code, Codex, Gemini, and Cursor) designed to help write modern, performant SwiftUI code. It provides guidance on avoiding common LLM mistakes regarding navigation, layout, animations, state management, and deprecated APIs. The skill includes references for accessibility (VoiceOver, Dynamic Type), modern API preferences (such as @Entry and @Observable), SwiftData configuration for CloudKit, and design consistency best practices.

Tokens
7.4K
Snippets
12
Records
51
Agent score
70%

What's inside swiftui-agent-skill

  1. Use the swiftui-pro skill for SwiftUI code reviews

    main

    The swiftui-pro skill is designed to comprehensively review Swift and SwiftUI code for correctness, modern API usage, maintainability, and performance. It targets iOS 26 and Swift 6.2 or later using modern Swift concurrency.

    When using this skill, you can provide an optional [focus area] as an argument to narrow the scope of the review. The skill follows a structured review process covering:

    • Deprecated APIs
    • View, modifier, and animation optimization
    • Data flow configuration
    • Navigation (e.g., NavigationStack, NavigationSplitView)
    • Accessibility (Dynamic Type, VoiceOver, Reduce Motion)
    • Performance and Swift code hygiene
  2. Use modern SwiftUI modifiers and APIs

    main

    When writing SwiftUI code, follow these modern API preferences to ensure compatibility with current standards and avoid deprecated patterns:

    • Styling: Use foregroundStyle() instead of foregroundColor().
    • Clipping: Use clipShape(.rect(cornerRadius:)) instead of cornerRadius().
    • Tabs: Use the Tab API instead of tabItem().
    • Haptics: Use sensoryFeedback() instead of UIKit APIs like UIImpactFeedbackGenerator.
    • Overlays: Prefer the trailing closure version overlay(alignment:content:) over the deprecated overlay(_:alignment:). Example: .overlay { Text("Hello") }.
    • Toolbar Placement: Use .topBarLeading and .topBarTrailing instead of the deprecated .navigationBarLeading and .navigationBarTrailing.
    • Scroll Indicators: Use .scrollIndicators(.hidden) instead of showsIndicators: false in the initializer.
    • Shapes: In iOS 17+, you can chain .fill() and .stroke() directly on a shape without using an overlay.
    • Images: Prefer the generated symbol asset API Image(.assetName) over string-based Image("assetName") when configured.
    • WebView: For iOS 26+, use the native WebView view type instead of wrapping WKWebView in UIViewRepresentable (requires import WebKit).
  3. Use the swiftui-pro agent skill for SwiftUI code reviews

    main

    The swiftui-pro skill is designed to comprehensively review Swift and SwiftUI code for correctness, modern API usage, and adherence to project conventions. It is optimized for reviewing, writing, or improving SwiftUI projects with a focus on iOS 26+ and Swift 6.2+.

    Core Review Principles

    • Target Environment: iOS 26 is the default deployment target; use Swift 6.2 or later with modern Swift concurrency.
    • Framework Preference: Prioritize SwiftUI; avoid UIKit unless explicitly requested.
    • Architecture: Break different types into separate Swift files rather than grouping multiple structs, classes, or enums in one file. Use a feature-based folder layout.
    • Dependencies: Do not introduce third-party frameworks without prior permission.

    Review Process

    The skill follows a structured review sequence covering:

    1. Deprecated APIs
    2. View, modifier, and animation optimization
    3. Data flow configuration
    4. Navigation performance (NavigationStack/NavigationSplitView)
    5. Design and Human Interface Guidelines compliance
    6. Accessibility (Dynamic Type, VoiceOver, Reduce Motion)
    7. Performance optimization
    8. Swift code validation (Concurrency, etc.)
    9. Code hygiene

    Note: For partial reviews, you can instruct the agent to load only specific relevant reference files (e.g., references/data.md).

  4. Configure Image Accessibility and VoiceOver

    main

    Ensure images are properly handled for VoiceOver users:

    • For decorative images, use Image(decorative:) or the .accessibilityHidden() modifier to prevent them from being read.
    • For meaningful images, always attach an .accessibilityLabel().
    • For buttons that use image labels, always include a text label. If you want the button to appear icon-only visually while remaining accessible to VoiceOver, apply the .labelStyle(.iconOnly) modifier.
    // Accessible icon-only button
    Button("Label", systemImage: "plus", action: myAction)
        .labelStyle(.iconOnly)
  5. Apply accessible typography and colors

    main

    Follow these best practices for typography and color to ensure accessibility and system compatibility:

    • Font Weight: Use .bold() instead of .fontWeight(.bold). This allows the system to select the optimal weight for the current context. Only use .fontWeight() for specific weights (like .medium or .semibold) when there is a strong requirement.
    • Font Sizes: Use .caption2 and .caption sparingly, as they are extremely small and can pose accessibility challenges.
    • Colors: Avoid using UIKit UIColor in SwiftUI code. Use SwiftUI Color or colors defined in the asset catalog.
  6. Configure SwiftData for CloudKit compatibility

    main

    If your project uses SwiftData with CloudKit, you must adhere to these constraints to prevent synchronization errors:

    • No Unique Attributes: Never use @Attribute(.unique).
    • Default Values/Optionals: All model properties must either have a default value or be marked as optional.
    • Optional Relationships: All relationships in your models must be marked as optional.
  7. Follow Swift coding best practices

    main

    When writing Swift code for SwiftUI projects, adhere to these modern language and API preferences:

    String and Foundation APIs

    • Use Swift-native string methods: replacing("a", with: "b") instead of replacingOccurrences(of: "a", with: "b").
    • Use modern Foundation APIs: URL.documentsDirectory instead of FileManager lookups, and appending(path:) for URL path manipulation.
    • Use localizedStandardContains() for filtering text based on user input.
    • Use Date.now instead of Date().
    • Use modern Date initializers for string conversion, e.g., Date(myString, strategy: .iso8601).
    • Use PersonNameComponents for formatting names instead of string interpolation.

    SwiftUI and UI Patterns

    • Prefer static member lookup over struct instances: .circle instead of Circle(), and .borderedProminent instead of BorderedProminentButtonStyle().
    • Use FormatStyle APIs for numbers instead of C-style formatting: Text(value, format: .number.precision(.fractionLength(2))) instead of String(format: "%.2f", value).
    • Avoid manual date formatting strings for user display; if necessary, use "y" instead of "yyyy" for years to ensure correct localization.
    • Note that import SwiftUI automatically provides access to UIImage or NSImage on their respective platforms; you do not need to explicitly import UIKit or import AppKit.

    Logic and Safety

    • Avoid force unwraps (!) and force try. Use if let, guard let, nil-coalescing, try?, or do-catch. If a failure is unrecoverable, use fatalError() with a clear description.
    • Use the if let value { shorthand instead of if let value = value {.
    • Omit return for single-expression functions.
    • Use if and switch as expressions when assigning values to variables.
    • Use count(where:) instead of filter().count when counting objects matching a predicate.
    • Prefer Double over CGFloat (except when using optionals or inout).
    • Make types conform to Comparable if they are repeatedly sorted using the same closure.
    • Do not swallow user-triggered errors silently (e.g., using print(error.localizedDescription)); show an alert instead.
    // Example: Using if/switch as expressions and omitting return
    var tileColor: Color {
        if isCorrect {
            .green
        } else {
            .red
        }
    }
  8. Maintain design consistency with shared constants

    main
    To ensure a uniform and consistent design throughout the app, avoid hard-coding values in individual views. Instead, place standard fonts, sizes, colors, stack spacing, padding, rounding, and animation timings into a shared enum of constants. This centralizes the design system, making it easier to adjust globally.