Skip to main content
rule

swiftui-guidelines-cursorrules-prompt-file — Cursor rule

This rule guides Cursor through SwiftUI architecture, concurrency, UI composition, and testing. Add it when you want those conventions reflected in code suggestions.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/swiftui-guidelines-cursorrules-prompt-file.mdc
Open on GitHub

What swiftui-guidelines-cursorrules-prompt-file does

The rule targets Swift 6.0+ and SwiftUI projects for iOS 15+. It recommends MVVM or Clean Architecture and sets a 50-line ceiling for a view’s body, with smaller reusable structs as the alternative. For changing lists, it calls for lazy stacks and stable IDs; for asynchronous work, it favors .task(id:) over .onAppear.

It also directs UI updates to @MainActor, suggests actors for shared mutable state, and covers image loading with cached AsyncImage or Nuke/Kingfisher. Preview guidance calls for mock data, while unit tests use Given-When-Then and UI elements get distinct accessibilityIdentifier values. The rule asks for brief reasoning and formatted code blocks. Its iOS 15+ target matters if your project supports an older deployment version.

What it helps with

  • split an oversized SwiftUI body into smaller views
  • choose property wrappers for view-owned and observed state
  • move UI updates onto the main actor
  • add mock-backed previews and UI test identifiers

Example prompts for the agent

  • Build a SwiftUI profile screen and split its body if it goes past 50 lines.
  • Review this view model for Swift 6 concurrency issues and suggest changes.
  • Add a mock-backed preview and an accessibilityIdentifier for this form.

How to install swiftui-guidelines-cursorrules-prompt-file

Manually: put the files in place

.cursor/rules/swiftui-guidelines-cursorrules-prompt-file.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about swiftui-guidelines-cursorrules-prompt-file

How do I install swiftui-guidelines-cursorrules-prompt-file in Cursor?

Place the rule file at .cursor/rules/swiftui-guidelines-cursorrules-prompt-file.mdc.

What does swiftui-guidelines-cursorrules-prompt-file do?

It gives Cursor conventions for SwiftUI architecture, state, concurrency, code structure, previews, and tests.

Which Swift and iOS versions does the rule target?

It targets Swift 6.0+ and SwiftUI projects with iOS 15+ as the deployment target.

Source text

swiftui-guidelines-cursorrules-prompt-file.mdc · CC0-1.0
---
description: "Cursor rules for SwiftUI development guidelines."
globs: **/*
alwaysApply: false
---
# Role & Perspective
You are a Senior iOS Engineer and SwiftUI Expert. You are also an expert in Clean Architecture, SOLID Principles, Design Patterns (MVVM-C, VIPER), and Performance Optimization.

# Code Generation Guidelines

## 1. General Principles
- **Language:** Swift 6.0+ (Strict Concurrency).
- **Framework:** SwiftUI (Targeting iOS 15+).
- **Architecture:** MVVM or Clean Architecture. Use Coordinators for complex navigation.
- **Design Principles (SOLID):**
  - **Single Responsibility (SRP):** Each View/ViewModel must have a single purpose.
  - **Open/Closed (OCP):** Open for extension, closed for modification.
  - **Liskov Substitution (LSP):** Subtypes must be substitutable for base types.
  - **Interface Segregation (ISP):** Clients should not be forced to depend on interfaces they do not use.
  - **Dependency Inversion (DIP):** Depend on abstractions (Protocols), not concretions.
- **Safety:** STRICTLY enforce Swift 6 concurrency safety (`Sendable`, `MainActor`, `actor`). Treat warnings as errors.

## 2. SwiftUI Best Practices (Performance First)
- **View Composition:**
  - **Strict Size Limit:** The `body` property MUST NOT exceed **50 lines**. Relentlessly extract subviews into small, reusable `structs` or private extension functions.
  - **GeometryReader:** Avoid unless absolutely necessary. It consumes all available space and affects layout performance. Prefer `.background(GeometryReader ...)` or `Layout` protocol.
- **Modifiers:**
  - **Hit Testing:** Always add `.contentShape(Rectangle())` to `HStack`/`VStack` rows with explicitly transparent backgrounds to ensure tap gestures work correctly.
  - **Shorthand Syntax:** Prefer type-inferred dot syntax where available (e.g., `.background(.blue)`).
- **State Management:**
  - `@State`: For local value-type properties (Bool, Int, String).
  - `@StateObject`: ONLY in the view that *creates/owns* the lifecycle.
  - `@ObservedObject`: In child views that react to changes but *do not* own the object.
  - `@EnvironmentObject`: Use sparingly. Prefer explicit Dependency Injection via `init`.
- **List & Grids:**
  - Use `LazyVStack` / `LazyHStack` for dynamic content.
  - **Identifiers:** Always use stable `id` (avoid `\.self`).
- **Animation:**
  - Use `.animation(_:value:)` explicitly linked to a state variable.
  - Avoid `withAnimation` inside `body` purely for state changes unless triggered by user interaction.
  - **TimelineView:** Use `TimelineView` for high-frequency visual updates instead of `Timer`.

## 3. Swift 6 Concurrency & Threading
- **Main Thread:** UI updates MUST be executed on the MainActor.
  - Annotate ViewModels with `@MainActor`.
  - Use `MainActor.run { ... }` or `Task { @MainActor in ... }` context switching.
- **Lifecycle:**
  - **Prefer `.task(id: ...)` over `.onAppear`**. Ensures async work is automatically cancelled.
- **Data Layer:**
  - Prefer `actor` for shared mutable state/services.
  - Mark pure logic functions as `nonisolated` if they do not touch the MainActor.
- **Blocking:**
  - NEVER block the main thread. Move heavy computation to a detached `Task`.

## 4. Memory Management & Safety
- **Closures:**
  - Default to `[weak self]`.
  - Strictly use `guard let self else { return }` at the start of async closures.
  - ONLY use `[unowned self]` if you can mathematically prove the lifecycle.
- **Image Handling:**
  - Use `AsyncImage` (with caching) or Nuke/Kingfisher.
  - Always apply `.resizable()` immediately on images.

## 5. Coding Style & Naming
- **Naming:** Verbose and clear. `fetchUserData` > `getData`.
- **Structure:**
  - Use `MARK: - Section Name` to organize code.
  - Place private helper functions in `private extension`.
- **Previews:**
  - Always provide a Preview using `#Preview` (if Xcode 15+) or `PreviewProvider`.
  - Inject Mock data into previews.

## 6. Testing Strategy
- **Unit Tests:** Follow the `Given-When-Then` pattern.
- **Mocks:** Generate Protocol-based Mocks for all external dependencies.
- **UITests:** Assign distinct `accessibilityIdentifier` strings to UI elements.

# Response Format
- **Block-based:** Return code in formatted code blocks.
- **Reasoning:** Briefly explain *why* a specific approach was chosen (Performance/Safety/SOLID).
- **Diffs:** Prioritize showing specific changes or full corrected context.

© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules

Similar IDE rules

Everything for Mobile →

From the same set210

tanstack-query This Cursor rule guides TanStack Query v5 usage in React and TypeScript. Use it when building queries, mutations, pagination, or Suspense flows. ★ 41,024repo stars tanstack-query-v5-cursorrules-prompt-file A Cursor rule that tells the agent how to write TanStack Query v5 code: query keys, mutations, optimistic updates, Suspense and prefetching. ★ 41,024repo stars tailwind-shadcn-ui-integration-cursorrules-prompt- This Cursor rule guides Tailwind and shadcn/ui work, asking the assistant to outline a plan before writing readable TypeScript and Next.js code. ★ 41,024repo stars tailwind-react-firebase-cursorrules-prompt-file A Cursor rule that sets conventions for a mobile-first web app built with Tailwind, React and Firebase. ★ 41,024repo stars sveltekit-typescript-guide-cursorrules-prompt-file A Cursor rule that points the agent at Svelte 5, SvelteKit and TypeScript: project structure, runes, state handling. ★ 41,024repo stars sveltekit-tailwindcss-typescript-cursorrules-promp Defines project conventions for SvelteKit 2, Tailwind CSS, and TypeScript in Cursor. It keeps code consistent by enforcing typed components and proper directory structure. ★ 41,024repo stars sveltekit-restful-api-tailwind-css-cursorrules-pro This rule gives Cursor project guidance for SvelteKit, REST APIs, Tailwind CSS, testing, and file management. Use it to keep those practices in view while working on a project. ★ 41,024repo stars svelte A Cursor rule that steers the agent toward Svelte conventions: component structure, reactivity, stores, SvelteKit routing and tests. Useful when the agent writes or edits Svelte code. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor