Prompt
Always use Swift’s built-in mechanisms for safely handling optional values instead of force unwrapping or manual nil handling. Specifically:
- Use optional binding (
if let,guard let) instead of checking for nil and force unwrapping:
// Avoid this pattern
if savedWindowFrame != nil {
let originalFrame = savedWindowFrame! // Risky force unwrap
}
// Prefer this pattern
if let savedWindowFrame {
let originalFrame = savedWindowFrame // Safe access
}
- Use collection transformation methods like
flatMaporcompactMapto handle optional collections elegantly:
// Avoid this pattern
return controllers.reduce([]) { result, c in
result + (c.surfaceTree.root?.leaves() ?? [])
}
// Prefer this pattern
return controllers.flatMap {
$0.surfaceTree.root?.leaves() ?? []
}
- Always add appropriate guards to check if objects exist within expected hierarchies or collections before performing operations:
@IBAction func toggleMaximize(_ sender: Any) {
guard let window = window else { return }
guard surfaceTree.contains(sender) else { return } // Check hierarchy membership
// Implementation...
}
These patterns make code more concise, readable, and less prone to runtime crashes due to unexpected nil values.