Created
December 10, 2014 08:47
-
-
Save stefanluptak/bccbc642701655c59ba3 to your computer and use it in GitHub Desktop.
How to write 'instancetype'-like code in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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