Skip to content

Instantly share code, notes, and snippets.

@stefanluptak
Created December 10, 2014 08:47
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 stefanluptak/bccbc642701655c59ba3 to your computer and use it in GitHub Desktop.
Save stefanluptak/bccbc642701655c59ba3 to your computer and use it in GitHub Desktop.
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