Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mateuszszklarek/fd8635fdb41bbac49ee1d809be9d257a to your computer and use it in GitHub Desktop.
Save mateuszszklarek/fd8635fdb41bbac49ee1d809be9d257a to your computer and use it in GitHub Desktop.
func forceCast<U>(_ value: Any?, to type: U.Type) -> U {
 return value as! U 
}
let value: Any? = 42
print(forceCast(value, to: Any.self))
// Prints "Optional(42)"
// (Prior to Swift 5, this would print "42".)
print(value as! Any)
// Prints "Optional(42)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment