Skip to content

Instantly share code, notes, and snippets.

@MaherKSantina
Created September 5, 2019 09:19
Show Gist options
  • Save MaherKSantina/95d9b2247064f04c18f4ad89a47a2387 to your computer and use it in GitHub Desktop.
Save MaherKSantina/95d9b2247064f04c18f4ad89a47a2387 to your computer and use it in GitHub Desktop.
A way to easily safely unwrap values
extension Optional {
/**
Unwraps a value or returns the default value provided if it's nil
- parameter defaultValue: The default value that will be returned if the original value is nil
*/
func unwrap(defaultValue: Wrapped) -> Wrapped {
guard let self = self else {
// Make the app crash in debug but not in production
assertionFailure("nil value")
return defaultValue
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment