Skip to content

Instantly share code, notes, and snippets.

@pofat
Last active December 1, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pofat/26ce5c952f584285d3fcc6778c62646c to your computer and use it in GitHub Desktop.
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 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