Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: swift-code-style/forced-unwrapped

Language: Unknown

Severity: Notice

Category: Code Style

Description

Declaring a variable as optional clearly signals that it may not hold a valid value and could be nil. Force-unwrapping bypasses that safety and will cause a runtime crash if the value is actually nil. Even if you check for nil first, relying on force-unwrapping is still discouraged because it undermines the intent of optionals. A safer and more maintainable approach is to use optional binding or optional chaining to handle values gracefully.

Non-Compliant Code Examples

var greeting: String?

// ...
println( \(greeting!))  // Noncompliant; could cause a runtime error

if greeting != nil {
  println( \(greeting!))  // Noncompliant; better but still not great
}

Compliant Code Examples

var greeting: String?

// ...
if let howdy = greeting {
  println(howdy)
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Integraciones sin problemas. Prueba Datadog Code Security