This chapter is taken from my book, "AI Assisted Coding for iOS Development". You can buy it to learn further about Cursor:
Working with Cursor is amazing. I am using Xcode since 2018, and barely used any other IDE so Cursor is the second IDE that I have spent so much time with.
To make my life easier, I started using the .cursorrules
in Cursor. You can add rules for all the chats and every time you Command+K, making it exactly what I need for my SwiftUI projects.
Why Custom Rules?
- Cursor uses Claude Sonnet 3.5 trained till April 2024, which knows about iOS 17 APIs. I can have Cursor always use
NavigationStack
instead ofNavigationView
, and similar APIs making my code upto date. - No callback hell please. No completion handlers. The rules ensure I am using async/await to deal with Swift 6 migration hell as well.
- I use Inject to update simulator without relying on previews so that I barely have to open Xcode, and minimise context switching between two IDEs.
Setting up your cursorrules
Create a new file in the root directory with the extension .cursorrules
. In that file, add your prompt. Here is a simple template to get you started:
You are an AI assistant specialized in SwiftUI development. Your role is to help developers write clean, efficient, and modern SwiftUI code. Follow these guidelines in all your responses:
## General Guidelines
1. Use the latest SwiftUI APIs and features whenever possible.
2. Implement async/await for asynchronous operations.
3. Write clean, readable, and well-structured code.
## Hot Reloading Setup
For all SwiftUI views, include the following to enable hot reloading:
1. Import the Inject framework.
2. Add the @ObserveInjection property wrapper to the view.
3. Use the .enableInjection() modifier in the main body of the view.
## Code Structure
When creating or modifying SwiftUI views, always use this structure if the view does not have it already:
```swift
import SwiftUI
import Inject
struct YourViewName: View {
@ObserveInjection var inject
// Other properties here
var body: some View {
// Your view content here
.enableInjection()
}
}
More SwiftUI Rules
While my rules focus mostly on getting it to work with hot reloading, you can take it a step further.
Here are Swift Cursor Rules by Ray and Lou:
These help write efficient, maintainable SwiftUI apps by emphasizing proper state management, performance optimization, and adherence to SwiftUI-specific patterns.
Moving Forward
With these rules in place, it is easier to update code with Cursor. You can always tweak these rules as your needs change to make the most out of it.
I have realised that Cursor has reduced the friction of using Claude on browser, and manually updating the code. To ship a ton, I need to spend less time coding, and more time apping instead.
For now, happy cursoring!