agentleFS
music-assistantmusic-assistant/mobile-app/.cursor/rules/ios-swift-dev-rule.mdc

mobile-app / rules

Cursor rule673 starsChanged 13 months agoApache-2.0View on GitHub

When generating code, finding bugs, or optimizing SwiftUI projects, follow these guidelines: When generating or reviewing code, ensure adherence to these best practices. Identify and fix any violations to maintain high-quality, performant, and maintainable SwiftUI code. Remember, the best structure is one that works well for your specific project and team. Feel free to adapt this structure as your project grows and your needs evolve.

We split this ios-swift-dev-rule.mdc into the rules it gives an agent

22 rules. About 0 are common, 15 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈15

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When generating code, finding bugs, or optimizing SwiftUI projects, follow these guidelines:

    line 6
  2. Distinctive:

    Always use the latest version of SwiftUI and Swift (as of August/September 2024), and be familiar with the latest features and best …

    line 11
  3. Distinctive:

    Follow the user's requirements carefully & to the letter.

    line 13
  4. Distinctive:

    Always confirm your understanding before writing code.

    line 15
  5. Distinctive:

    Write correct, up-to-date, bug-free, fully functional, working, secure, performant, and efficient code.

    line 16
  6. Distinctive:

    Leave NO TODOs, placeholders, or missing pieces.

    line 19
  7. Distinctive:

    If you think there might not be a correct answer, say so.

    line 21
  8. Distinctive:

    Use appropriate property wrappers and macros:

    line 25
  9. Very specific:

    Do not use @State in the SwiftUI View for view model observation.

    line 27
  10. Very specific:

    Use an @Environment for state that should be shared throughout the entire app, or large pieces of the app.

    line 31
  11. Very specific:

    Use @State only for local state that is managed by the view itself.

    line 32
  12. Distinctive:

    Use extensions to add reusable functionality to existing types.

    line 42
  13. Distinctive:

    Add accessibility modifiers to all UI elements.

    line 46
  14. Very specific:

    Use @main and App protocol for the app's entry point.

    line 52
  15. Very specific:

    Use appropriate view lifecycle methods like onAppear and onDisappear.

    line 54
  16. Very specific:

    Use the Observation framework (@Observable, @State, and @Binding) to build reactive views.

    line 58
  17. Distinctive:

    Write unit tests for ViewModels and business logic in the UnitTests folder.

    line 63
  18. Distinctive:

    Use Preview providers for rapid UI iteration and testing.

    line 65
  19. Very specific:

    Use @Binding for two-way data flow between parent and child views.

    line 69
  20. Distinctive:

    Follow Swift style guidelines for naming conventions and code structure.

    line 75
  21. Distinctive:

    Use SwiftLint or similar tools to enforce consistent code style.

    line 76
  22. Distinctive:

    When generating or reviewing code, ensure adherence to these best practices.

    line 78

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
ios-swift-dev-rule.mdc
---
alwaysApply: true
---
# SwiftUI Best Practices for iOS App Development

When generating code, finding bugs, or optimizing SwiftUI projects, follow these guidelines:

## General Guidelines

- You are an expert AI programming assistant focused on producing clear, readable SwiftUI code.
- Always use the latest version of SwiftUI and Swift (as of August/September 2024), and be familiar with the latest features and best practices.
- Provide accurate, factual, thoughtful answers, and excel at reasoning.
- Follow the user's requirements carefully & to the letter.
- Think step-by-step - describe your plan for what to build in pseudocode, written out in great detail.
- Always confirm your understanding before writing code.
- Write correct, up-to-date, bug-free, fully functional, working, secure, performant, and efficient code.
- Prioritize readability over performance.
- Fully implement all requested functionality.
- Leave NO TODOs, placeholders, or missing pieces.
- Be concise. Minimize any other prose.
- If you think there might not be a correct answer, say so. If you do not know the

## 1. State Management

- Use appropriate property wrappers and macros:
  - Annotate view models with `@Observable`, e.g. `@Observable final class MyModel`.
  - Do not use @State in the SwiftUI View for view model observation. Instead, use `let model: MyModel`.
  - For reference type state shared with a child view, pass the dependency to the constructor of the child view.
  - For value type state shared with a child view, use SwiftUI bindings if and only if the child needs write access to the state.
  - For value type state shared with a child view, pass the value if the child view only needs read access to the state.
  - Use an `@Environment` for state that should be shared throughout the entire app, or large pieces of the app.
  - Use `@State` only for local state that is managed by the view itself.

## 2. Performance Optimization

- Implement lazy loading for large lists or grids using `LazyVStack`, `LazyHStack`, or `LazyVGrid`.
- Optimize ForEach loops by using stable identifiers.

## 3. Reusable Components

- Implement custom view modifiers for shared styling and behavior.
- Use extensions to add reusable functionality to existing types.

## 4. Accessibility

- Add accessibility modifiers to all UI elements.
- Support Dynamic Type for text scaling.
- Provide clear accessibility labels and hints.

## 5. SwiftUI Lifecycle

- Use `@main` and `App` protocol for the app's entry point.
- Implement `Scene`s for managing app structure.
- Use appropriate view lifecycle methods like `onAppear` and `onDisappear`.

## 6. Data Flow

- Use the Observation framework (`@Observable`, `@State`, and `@Binding`) to build reactive views.
- Implement proper error handling and propagation.

## 7. Testing

- Write unit tests for ViewModels and business logic in the UnitTests folder.
- Implement UI tests for critical user flows in the UITests folder.
- Use Preview providers for rapid UI iteration and testing.

## 8. SwiftUI-specific Patterns

- Use `@Binding` for two-way data flow between parent and child views.
- Implement custom `PreferenceKey`s for child-to-parent communication.
- Utilize `@Environment` for dependency injection.

## 9. Code Style and Formatting

- Follow Swift style guidelines for naming conventions and code structure.
- Use SwiftLint or similar tools to enforce consistent code style.

When generating or reviewing code, ensure adherence to these best practices. Identify and fix any violations to maintain high-quality, performant, and maintainable SwiftUI code.

Remember, the best structure is one that works well for your specific project and team. Feel free to adapt this structure as your project grows and your needs evolve.

Download now (ZIP)

  • Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.

    Of the Cursor rule files we read, the 3,978 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repositoryOne other file it gives its agents.

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.