Skip to content

Instantly share code, notes, and snippets.

@bluwave
Forked from jackhl/typecasttweet.swift
Created April 7, 2016 07:33
Show Gist options
  • Select an option

  • Save bluwave/0991b734f3fbb16a55b3619124c7ee92 to your computer and use it in GitHub Desktop.

Select an option

Save bluwave/0991b734f3fbb16a55b3619124c7ee92 to your computer and use it in GitHub Desktop.
// only class types:
func myCast<MyClass: AnyObject>(value: AnyObject, type: MyClass.Type) -> MyClass? {
return value as? MyClass
}
// any type (class, struct, etc):
func myCast1<MyClass: Any>(value: Any, type: MyClass.Type) -> MyClass? {
return value as? MyClass
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment