Created
September 5, 2019 09:19
-
-
Save MaherKSantina/95d9b2247064f04c18f4ad89a47a2387 to your computer and use it in GitHub Desktop.
A way to easily safely unwrap values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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