Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to write 'instancetype'-like code in Swift
import Foundation
import UIKit
extension UIViewController {
class func fromMainStoryboard () -> UIViewController {
var storyboardID = NSStringFromClass(self)
let dotRangeOptional = storyboardID.rangeOfString(".", options: NSStringCompareOptions.allZeros)
if let dotRange = dotRangeOptional {
storyboardID = storyboardID.substringFromIndex(dotRange.startIndex.successor())
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewControllerWithIdentifier(storyboardID) as UIViewController
class SomeViewController: UIViewController {}
let someVC = SomeViewController.fromMainStoryboard() // returns UIViewController ... I want it to be SomeViewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment