Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created March 31, 2016 14:34
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 wh1pch81n/be2720f7badf3fbcbf17291811e5ff62 to your computer and use it in GitHub Desktop.
Save wh1pch81n/be2720f7badf3fbcbf17291811e5ff62 to your computer and use it in GitHub Desktop.
In swift, if you instantiate the view controller from the storyboard identifier, you typically need to cast it. However, since the storyboard identifier is typically the same as the vc name, it might be perfectly acceptable to just pass in the type and convert that to a string. Since you passed in a type, the function can infer the return type.
extension UIStoryboard {
func instantiateViewControllerWithTypeAsIdentifier<T>(t: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(t)) as! T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment