Last active
December 1, 2016 13:10
-
-
Save pofat/26ce5c952f584285d3fcc6778c62646c to your computer and use it in GitHub Desktop.
A force unwrapper for Optional type with error hint, suitable for development
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
// This trick is orininally from 'https://boxueio.com/' | |
infix operator !! | |
func !!<T>(optional: T?, errorMessage: @autoclosure () -> String) -> T { | |
if let value = optional { return value } | |
fatalError(errorMessage) | |
} | |
let record = ["foo": "bar"] | |
// Run it and you will get a run time error with customized message 'Such key does not exist' | |
record["name"] !! "Such key does not exist" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment