Rule Swift

Optional Chaining

Use optional chaining and nil coalescing instead of force unwrapping

swiftsafetyoptionalsmacosios
CLAUDE.md

Never force-unwrap optionals with !. Use optional chaining (?.), nil coalescing (??), or guard let / if let binding.

// Good
let name = user?.profile?.displayName ?? "Anonymous"
let count = items?.count ?? 0

// Good — guard for early exit
guard let window = NSApp.mainWindow else { return }

// Bad — crashes if nil
let name = user!.profile!.displayName

The only acceptable use of ! is IBOutlet properties in storyboard-based UIKit code, and even then prefer programmatic UI.

Copy this block into your CLAUDE.md or agent config file to enforce it in your workflow.

get crystl