Skip to content

Instantly share code, notes, and snippets.

@christianhatch
christianhatch / storyboard-viewcontroller-init.swift
Last active January 27, 2022 22:27
A simple UIStoryboard extension to easily instantiate viewcontrollers using Swift type casting. Raw
// Do you often find yourself writing lines of code that look like this?
// let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as! ViewController
// Here's a simpler way, using generics and Swift type-casting (as long as the viewcontroller's storyboard identifier is its class name).
// Now you can write: let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController() as ViewController
extension UIStoryboard {
func instantiateViewController<T: UIViewController>() -> T {
guard let vc = instantiateViewControllerWithIdentifier(String(T)) as? T else {
fatalError("Could not locate viewcontroller with with identifier \(String(T)) in storyboard.")
}
#########################
# .gitignore file for Xcode4 / OS X Source projects
#
# Version 2.0
# For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects
#
# 2013 updates:
# - fixed the broken "save personal Schemes"
#
# NB: if you are storing "built" products, this WILL NOT WORK,