Skip to content

Instantly share code, notes, and snippets.

@endash
Created June 5, 2015 20:39
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 endash/ea75a61c90de30a4bb73 to your computer and use it in GitHub Desktop.
Save endash/ea75a61c90de30a4bb73 to your computer and use it in GitHub Desktop.
Instantiate a view from a nib/xib in Swift, with the current API
extension NSView {
class func instantiateViewFromNib<T>(named nibName: String, inBundle bundle: NSBundle = NSBundle.mainBundle()) -> T? {
var objects : NSArray?
if bundle.loadNibNamed(nibName, owner: nil, topLevelObjects: &objects) {
if let objects = objects {
for object in objects {
if let object = object as? T {
return object
}
}
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment