Skip to content

Instantly share code, notes, and snippets.

@mhuusko5
Created October 19, 2015 12:30
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 mhuusko5/2191f4054f22682f0af1 to your computer and use it in GitHub Desktop.
Save mhuusko5/2191f4054f22682f0af1 to your computer and use it in GitHub Desktop.
Swift – func ensureType<T>(type type: T.Type = T.self, transform: (Any) -> T)(_ any: Any) -> T
func ensureType<T>(type type: T.Type = T.self, transform: (Any) -> T)(_ any: Any) -> T {
return ensureType(any, type: type, transform: transform)
}
func ensureType<T>(any: Any, type: T.Type = T.self, transform: (Any) -> T) -> T {
if let any = any as? T {
return any
} else {
return transform(any)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment