Always use Swift's built-in mechanisms for safely handling optional values instead of force unwrapping or manual nil handling. Specifically: 1. Use optional binding (`if let`, `guard let`) instead of checking for nil and force unwrapping:
Always use Swift’s built-in mechanisms for safely handling optional values instead of force unwrapping or manual nil handling. Specifically:
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
}
flatMap
or compactMap
to 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() ?? []
}
@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.
Enter the URL of a public GitHub repository