Skip to content

Instantly share code, notes, and snippets.

@radex
Last active August 29, 2015 14:05
Show Gist options
  • Save radex/5e4b3dbdcdfda4c32718 to your computer and use it in GitHub Desktop.
Save radex/5e4b3dbdcdfda4c32718 to your computer and use it in GitHub Desktop.
Swift extensions: NSStoryboard

I enjoy making and playing with little DSL-ish extensions for Cocoa types in Swift. For instance, the code below allows me to write cool

storyboard.VC["foo_bar"]
storyboard.VC.initial

instead of:

storyboard.instantiateControllerWithIdentifier("foo_bar") as NSViewController
storyboard.instantiateInitialController() as NSViewController

Of course I could've just as well extended NSStoryboard with a method like .getVC(), but this is all just for fun anyway ;)


Previously in Swift Extensions:

import Cocoa
class NSStoryboardVCGetterProxy {
private weak var storyboard: NSStoryboard!
private init(storyboard: NSStoryboard) {
self.storyboard = storyboard
}
subscript(identifier: String) -> NSViewController {
return storyboard.instantiateControllerWithIdentifier(identifier) as NSViewController
}
var initial: NSViewController {
return storyboard.instantiateInitialController() as NSViewController
}
}
extension NSStoryboard {
var VC: NSStoryboardVCGetterProxy {
return NSStoryboardVCGetterProxy(storyboard: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment